Archiv für die Kategorie ‘.NET’

Rekursiv vs. Iterativ

Sonntag, 29. Januar 2012

In der dotnetpro 8/2011 hat Ralp Westphal im Artikel “Aus Baum mach Fluss” zum Einstieg die Fakultätsberechnung in der rekursiven und in der iterativen Variante gezeigt. Seiner Meinung nach ist die iterative Variante einfacher zu verstehen, besonders für nicht-Mathematiker.
Ich fragte mich dann, welche Variante ist für den Computer einfacher zu verstehen, d.h. welche Variante ist performanter? Oder ist es egal, optimiert der Compiler schon so weit, dass beide Varianten in etwa gleich schnell sind?

Ich habe ein kleines Programm geschrieben, dass die Fakultät für verschiedene Werte mehrmals berechnet, um messbare Werte zu erhalten.
Hier die Resultate:

Factorial recursive and iterative
---------------------------------

10!
Recursive: 00:00:00.0390023
Iterative: 00:00:00.0220012

100!
Recursive: 00:00:00.2940168
Iterative: 00:00:00.1720099

1000!
Recursive: 00:00:02.5131437
Iterative: 00:00:01.0270588

10000!
Recursive: 00:00:25.0624335
Iterative: 00:00:09.4535407

Die rekursive Variante braucht also etwa 2.5 mal so lang wie die iterative. Und bei grossen Fakultäten kommt noch ein weiteres Problem hinzu: Es wird eine StackOverflowException geworfen, da jeder Rekursionsschritt einen Funktionsaufruf zur Folge hat, der einen Teil des Stacks beansprucht.

Fazit:
Der iterative Ansatz ist (mindestens in diesem Fall) auch für den Computer “einfacher zu verstehen”. Neben der besseren Performance ist aber auch die gebannte Gefahr des Stack Overflows ein wichtiger Grund, die iterative Varinate vorzuziehen.

Factorial Source Code

Slippery When Wet #5: The Process object or Optimization? don’t do it!

Samstag, 12. November 2011

Slippery When WetI proudly present to you the fifth in a infinite number of posts of “Slippery When Wet.” In these posts I show you a little bastard I stumbled on.

To get the memory usage information you can use the System.Diagnostics.Process class. As I wantet to check the memory usage regularly I kept the reference to the Process object in my class. But the value never changed anymore… Then I tried to get the Process object every time i needed the current memory size – and it worked.

So I read the documentation a little more carefully and got to this part:

The process component obtains information about a group of properties all at once. After the Process component has obtained information about one member of any group, it will cache the values for the other properties in that group and not obtain new information about the other members of the group until you call the Refresh method. Therefore, a property value is not guaranteed to be any newer than the last call to the Refresh method. The group breakdowns are operating-system dependent.

So I changed my code again to not get the Process every time I wanted the current memory size but call the Refresh method.

And what did we learn from this episode?

Michael A. Jackson (“The First Rule of Program Optimization: Don’t do it (…)“) and Donald Knuth (“(…) premature optimization is the root of all evil (…)“) are right. Don’t do optimization, especially not if you don’t really know what you are doing…

.NET Reflectors Erben

Mittwoch, 18. Mai 2011

Nachdem redgate Anfangs Februar Ankündigte, dass der .NET Reflector kostenpflichtig wird, haben sich mehrere Nachfolger empfohlen:

Als erstes kündigte das Team von #develop das Tool ILSpy an.

Danach folgte Telerik mit JustDecompile.

JetBrains, welche sich auch für … verantwortlich zeichnen, kündigten dann auch noch einen Erben an, das Tool dotPeek.

Redgate kündigte währenddessen an, dass es weiterhin eine kostenlose Version von .NET Reflector geben wird. Es ist in diesem Bereich also noch einiges in Bewegung…

Get the password of user IWAM_

Samstag, 17. Januar 2009

After trying some things in the IIS settings I wanted to change back to use the IWAM_* account. But for this I needed the password. I’ve searched and found some informations and tips to get the password that was used for the IUSR_* or IWAM_* accounts.

Auswahl der zu benutzenden Framework-Version

Mittwoch, 12. November 2008

Um eine .net-Applikation mit einer bestimmten Framework-Version auszuführen, kann diese im App.Conifg-File angegeben werden. Mit folgendem Beispiel wird die Anwendung mit dem Framework 1.1 ausgeführt, wenn man den Kommentar um das andere supportedRuntime-Tag setzt, wird das Framework 2.0 verwenden.

< ?xml version="1.0" encoding="utf-8">
<configuration>
   <startup>
      <supportedruntime version="v1.1.4322"/>
<!--      <supportedRuntime version="v2.0.50727"/>-->
   </startup>
</configuration>

Die Datei muss dabei dem Dateinamen der Anwendung mit angehängtem .config entsprechen, z.B. für die Anwendung “MyApp.exe” heisst die Datei “MyApp.exe.config”.

Slippery When Wet #2: SCOPE_IDENTITY and SqlDataReader

Samstag, 25. Oktober 2008

I proudly present to you the second in a infinite number of posts of “Slippery When Wet.” In these posts I show you a little bastard I stubled on.

Imagine you have a database table with a cloumn id of the data type integer that has set the IDENTITY. You use a stored procedure to insert a new entry into this table. Inside this stored procedure you use the SCOPE_IDENTITY() function to get the identifier created for this row. With RETURN SCOPE_IDENTITY() you give the identity to the caller.

You call this stored procedure from a SqlComand with ExecuteReader() that returns you an SqlDataReader object.

From this SqlDataReader you read now the identifier with GetInt32().

Wrong !

This will give you an InvalidCastException. SCOPE_IDENTITY()’s return type is numeric, although your identifier column is an integer. SqlDataReader’s GetXY functions do not convert the data and throw the exception when the data is not already of the right type.

The first solution

You can read the value with GetDecimal() and cast the value to an int:

int identifier = (int)reader.GetDecimal(0);

The second solution

You cast the identifier inside the stored procedure und return it already as an integer:

SELECT CAST(SCOPE_IDENTITY() AS INT)

Whatever solution you choose, take care that you use always the same inside your application.

Exception after sending data async from a thread

Samstag, 24. Mai 2008

If you have a situation like this:

void MyThreadFunc()
{
// do some work

socket.BeginSend(buffer, 0, buffer.Length, 0, new AsyncCallback(MyCallback), s);
}

void MyCallback(IAsyncResult ar)
{
Socket s = (Socket) ar.AsyncState;
s.EndSend(ar);
}

And you send a big buffer over a slow connection it can happen that calling EndSend() will throw a SocketException ‘The I/O operation has been aborted because of either a thread exit or an application request’.

This happens because the Thread that started the sending is no more available when the sending ends. The only solution I know is to make sure the thread is still available or to use syncronous sending if possible.

Benutzen der NAnt-Properties

Mittwoch, 21. November 2007

Der Zugriff auf die NAnt-Properties ist meiner Meinung nach etwas unglücklich gewählt, da zwei verschiedene Varianten je nach Kontext benutzt werden müssen.

Folgendes Beispiel zeigt sehr schön die beiden Varianten, wie NAnt-Properties im jeweiligen Zusammenhang angewendet werden müssen:

<delete file="${Directory}${File}.new"
if="${file::exists(Directory + '' + File + '.new')}" />

Dieser Befehl löscht die Datei, falls sie vorhanden ist. Für den Dateinamen muss die Syntax mit dem ${} benutz werden, die so genannte ‘property expansion’.

Für die Bedingung wird dann die Methode für den Zugriff auf das Property innerhalb einer Expression benutzt. Dabei wird auf ${} verzichtet.

Exception “No web service found at:” with /js or /jsdebug

Freitag, 09. März 2007

With “ASP.NET 2.0 AJAX Extensions 1.0″ (I love this name with two version numbers inside…) you can request a JavaScript Proxy for a web service with appending /js or /jsdebug to the web service path, like “http://my-server/my-service.asmx/js”.

When I deployed my application I’ve got the InvalidOperationException “No web service found at:”. When I called the web service without /js or /jsdebug the web service worked (at least the generated forms in the Browser where shown).

I’ve spent a lot of time ’til I recognized the File my-service.asmx was not included in the deploy package. So without the /js appended the my-service.asmx file was not needed, the code from the compiled dll was executed directly, but when I added /js it needed the file.

Conclusion:
Don’t forget to add the .asmx files in the deployment of your ASP.NET AJAX Project!

Verhalten des System.Timers.Timer bei neusetzen des Intervalles

Dienstag, 31. Oktober 2006

Nur als kleine Gedächnisstütze für mich:

Wenn man beim System.Timers.Timer das Interval neu setzt, wird dieses nach dem nächsten Auslösen des Timers verwendet:

Kurzer Bespielcode:

using System;
public class MyClass
{
  private static System.Timers.Timer timer = new System.Timers.Timer();

  public static void Main()
  {
    timer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimer);
    timer.Interval = 3000;
    timer.Start();

    System.Threading.Thread.Sleep(7000);
    timer.Interval = 2000;

    Console.ReadLine();
  }

  protected static void OnTimer(object sender, System.Timers.ElapsedEventArgs e)
  {
    Console.WriteLine(DateTime.Now.ToString());
  }
}