Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
  1. It doesn't often given you the performance you expect. See this question on SO, where optimizations have gone wrongoptimizations have gone wrong. In fact it can have adverse effects. That's the problem with undocumented behaviour.

  2. Mostly modern compilers will do it for you anyway.

  1. It doesn't often given you the performance you expect. See this question on SO, where optimizations have gone wrong. In fact it can have adverse effects. That's the problem with undocumented behaviour.

  2. Mostly modern compilers will do it for you anyway.

  1. It doesn't often given you the performance you expect. See this question on SO, where optimizations have gone wrong. In fact it can have adverse effects. That's the problem with undocumented behaviour.

  2. Mostly modern compilers will do it for you anyway.

added 64 characters in body
Source Link
nawfal
  • 157
  • 2
  • 10

1. if (Dictionary<K, V>.TryGetValue(K, out V)) do something with V

instead of

    if (Dictionary<K, V>.ContainsKey(K))
        do something with Dictionary<K, V>[K]
  1.   DirectoryInfo.EnumerateFiles();
    

    instead of

      DirectoryInfo.GetFiles();
    
  2. For reference types:

     s = o as string;
     if (s != null)
         proceed
    

    instead of

     if (o is string)
         s = (string)o;
    
  3.   if (counter < X || expensiveFunction())
    

    instead of

      if (expensiveFunction() || counter < X)
    
  4.   void M<T>(T o) //avoids boxing
      {
    
      }
    

    instead of

      void M(object o)
      {
    
      }
    
  1. Double lookup

     if (Dictionary<K, V>.TryGetValue(K, out V))
         do something with V
    

    instead of

     if (Dictionary<K, V>.ContainsKey(K))
         do something with Dictionary<K, V>[K]
    
  2. Load 'em all

     DirectoryInfo.EnumerateFiles();
    

    instead of

     DirectoryInfo.GetFiles();
    
  3. Two stage casting:

     s = o as string;
     if (s != null)
         proceed
    

    instead of

     if (o is string)
         s = (string)o;
    
  4. If the order doesn't matter

     if (counter < X || expensiveFunction())
    

    instead of

     if (expensiveFunction() || counter < X)
    
  5. Boxing

     void M<T>(T o) //avoids boxing
     {
    
     }
    

    instead of

     void M(object o)
     {
    
     }
    

1. if (Dictionary<K, V>.TryGetValue(K, out V)) do something with V

instead of

    if (Dictionary<K, V>.ContainsKey(K))
        do something with Dictionary<K, V>[K]
  1.   DirectoryInfo.EnumerateFiles();
    

    instead of

      DirectoryInfo.GetFiles();
    
  2. For reference types:

     s = o as string;
     if (s != null)
         proceed
    

    instead of

     if (o is string)
         s = (string)o;
    
  3.   if (counter < X || expensiveFunction())
    

    instead of

      if (expensiveFunction() || counter < X)
    
  4.   void M<T>(T o) //avoids boxing
      {
    
      }
    

    instead of

      void M(object o)
      {
    
      }
    
  1. Double lookup

     if (Dictionary<K, V>.TryGetValue(K, out V))
         do something with V
    

    instead of

     if (Dictionary<K, V>.ContainsKey(K))
         do something with Dictionary<K, V>[K]
    
  2. Load 'em all

     DirectoryInfo.EnumerateFiles();
    

    instead of

     DirectoryInfo.GetFiles();
    
  3. Two stage casting:

     s = o as string;
     if (s != null)
         proceed
    

    instead of

     if (o is string)
         s = (string)o;
    
  4. If the order doesn't matter

     if (counter < X || expensiveFunction())
    

    instead of

     if (expensiveFunction() || counter < X)
    
  5. Boxing

     void M<T>(T o) //avoids boxing
     {
    
     }
    

    instead of

     void M(object o)
     {
    
     }
    
added 153 characters in body
Source Link
nawfal
  • 157
  • 2
  • 10
  1. Your dependencies to a specific problem can change and any time spent on optimizing before completing the logical section of your application is a waste of time. I mean optimizing at a relatively early stage. Today you have an List<T> and by the time your app ships you had to change it to LinkedList<T> and now all the benchmarking was a waste of time and effort.

  2. Mostly the real bottleneck of your application (read as measurable difference) could be 5% of your code (mostly the sql ones) and optimizing the other 95% doesn't give your customers any benefit.

  3. Usually "technically" better performing code means more verbosity which in turn means more error prone code which in turn means harder maintainability and more time spent which in turn means you earn less money.

  4. The carbon footprint you save for the whole world thru 1% performance gain is easily dwarfed by greenhouse gases your team will have to emit on maintaining debugging and maintaining that code.

  1.   DirectoryInfo.EnumerateFiles();
    

    instead of

      DirectoryInfo.GetFiles();
    
  2. For reference types:

     s = o as string;
     if (s != null)
         proceed
    

    instead of

     if (o is string)
         s = (string)o;
    
  3.   if (counter < X || expensiveFunction())
    

    instead of

      if (expensiveFunction() || counter < X)
    
  4.   void M<T>(T o) //avoids boxing
      {
    
      }
    

    instead of

      void M(object o)
      {
    
      }
    
  1. Your dependencies to a specific problem can change and any time spent on optimizing before completing the logical section of your application is a waste of time. I mean optimizing at a relatively early stage. Today you have an List<T> and by the time your app ships you had to change it to LinkedList<T> and now all the benchmarking was a waste of time and effort.

  2. Mostly the real bottleneck of your application (read as measurable difference) could be 5% of your code (mostly the sql ones) and optimizing the other 95% doesn't give your customers any benefit.

  3. Usually "technically" better performing code means more verbosity which in turn means more error prone code which in turn means harder maintainability and more time spent which in turn means you earn less money.

  4. The carbon footprint you save for the whole world thru 1% performance gain is easily dwarfed by greenhouse gases your team will have to emit on maintaining debugging that code.

  1.   DirectoryInfo.EnumerateFiles();
    

    instead of

      DirectoryInfo.GetFiles();
    
  2. For reference types:

     s = o as string;
     if (s != null)
         proceed
    

    instead of

     if (o is string)
         s = (string)o;
    
  3.   if (counter < X || expensiveFunction())
    

    instead of

      if (expensiveFunction() || counter < X)
    
  1. Your dependencies to a specific problem can change and any time spent on optimizing before completing the logical section of your application is a waste of time. I mean optimizing at a relatively early stage. Today you have an List<T> and by the time your app ships you had to change it to LinkedList<T> and now all the benchmarking was a waste of time and effort.

  2. Mostly the real bottleneck of your application (read as measurable difference) could be 5% of your code (mostly the sql ones) and optimizing the other 95% doesn't give your customers any benefit.

  3. Usually "technically" better performing code means more verbosity which in turn means more error prone code which in turn means harder maintainability and more time spent which in turn means you earn less money.

  4. The carbon footprint you save for the whole world thru 1% performance gain is easily dwarfed by greenhouse gases your team will have to emit on debugging and maintaining that code.

  1.   DirectoryInfo.EnumerateFiles();
    

    instead of

      DirectoryInfo.GetFiles();
    
  2. For reference types:

     s = o as string;
     if (s != null)
         proceed
    

    instead of

     if (o is string)
         s = (string)o;
    
  3.   if (counter < X || expensiveFunction())
    

    instead of

      if (expensiveFunction() || counter < X)
    
  4.   void M<T>(T o) //avoids boxing
      {
    
      }
    

    instead of

      void M(object o)
      {
    
      }
    
Source Link
nawfal
  • 157
  • 2
  • 10
Loading
Post Made Community Wiki by nawfal