having fun with code

Unnecessary function calls inside loops

Calling to functions that return the same value over and over to test the exit condition on a loop is a very novel error or mistake, but I have seen it multiple times in different programming languages. At the end, the problem is the programmer relying on the compiler optimization, which I think is a bad practice. Write efficient code and let the compiler do its job. But don’t rely on it to optimize your inefficient code. Not to say than there is no compiler optimization at all when writing interpreted code like Python or Javascript.

Here is a simple test to show how adding a simple string.length call on a loop impacts the performance.

  1. var text = "Calling to functions that return the same value over and over to test the exit condition on a loop is a very novel error or mistake, but I have seen it multiple times in different programming languages. At the end, the problem is the programmer relying on the compiler optimization, which I think is a bad practice. Write efficient code and let the compiler do its job. But don't rely on it to optimize your inefficient code. Not to say than there is no compiler optimization at all when writing interpreted code like Python or Javascript.";
  2. var iterations = 100000;
  3.  
  4. var start_slow = (new Date()).getTime();
  5. for (var n=0; n<iterations; n++) {
  6.   for (var i=0; i<text.length; i++) { // Call to text.length inside loop is executed N times
  7.     // Nothing to do
  8.   }
  9. }
  10. var end_slow = (new Date()).getTime();
  11.  
  12. var start_fast = (new Date()).getTime();
  13. var l = text.length;    // Call to text.length outside the loop is executed only once
  14. for (var n=0; n<iterations; n++) {
  15.   for (var i=0; i<l; i++) {
  16.     // Nothing to do
  17.   }
  18. }
  19. var end_fast = (new Date()).getTime();
  20.  
  21. console.log('Slow:' + (end_slow – start_slow));
  22. console.log('Fast:' + (end_fast – start_fast));

Note: I have tested it on Firefox 3.0 and Safari 3.2. The second seems to have a bigger performance impact.
Firefox: Slow: 3,667ms Fast: 3,342ms
Safari: Slow: 6,273ms Fast: 2,157ms
Demo: http://code.enekoalonso.com/js/tests/strlen-loop-test.html

Related Posts:

  • No Related Post

2 Comments to Unnecessary function calls inside loops

  1. December 3, 2008 at 08:59 | Permalink

    Well, in fact oriented object languages use to have good implementations for “length” and similar functions (using a variable where length is stored, so once is computed, access is O(1)), no matter if they are interpreted or compiled.

    I’m impressed about your example on Safari, I though the implementation of string length would be good.

    About ‘don’t rely on compilers to optimize your programming faults’, check this other article:
    Un error comun en programacion: lenguages compilados

    The conclusion for G++ is: the compiler is able to optimize strlen, but not other functions that could be optimized as well.

    So as you said, is not a good idea to rely or interpreters or compilers.

Leave a Reply

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Additional comments powered by BackType

About the blog

This is a blog about development, focused mainly on Javascript but also other languages like python, shell scripts and more.

About the author

Eneko Alonso is a software engineer and UI developer with more than eight years of experience in software and web development. He lives in San Luis Obispo, California and works at LEVEL Studios.

Contact Info

Contact Info