harriyott.com

Friday, February 22, 2008

Lines of code != running time

I've just realised I do a bizarre thing in my subconscious when I'm coding. Now I'm all .NET 3.5, I've been writing code like:

feeds.ForEach(f => ProcessFeed(f));

One line of code instead of the usual four (OK, so two lines were the curly brackets) to write a foreach loop.

Now the thing is, I've unwittingly felt like the code now runs faster because the method is shorter. Of course that isn't true, but having nicer, shorter, eleganter, maintainabler code feels like it should be more efficient too.

1 Comments:

Blogger flip said...

Actually, often times that code can be /much/ faster -- depends on the system you're working in.

The concise version is more descriptive, which lets the interpreter/compiler make better choices... A disastrous mistake people learning Mathematica/Lisp/Functional Programming languages make is to still write for(i=0;i<n;i++) loops instead of using arrays/lists/map() statements, which KILL this type of efficient vectorization.

March 10, 2008 10:39 AM  

Post a Comment

Links to this post:

Create a Link

<< Home