[Metakit] Performance comparison Q

Jeff Godfrey jeff_godfrey at pobox.com
Mon May 9 13:40:52 CEST 2005


---- Original Message ----- 
From: "Jean-Claude Wippler" <jcw at equi4.com>

> The C code is pretty obvious:
>
>     int sum = 0; for (i = 0; i < 50000; ++i) sum += data[i];
>
> This one in tcl 8.4.6 runs at quite a bit under 1% of that speed:
>
>     set sum 0; foreach x $data { incr sum $x }

JC,

I'm a bit confused by the above comparison...

The C loop is just that - a loop that adds numbers in the range of 0-49999. 
The TCL example is much more than that, in that it must first "retrieve" the 
next number from some data structure (a list in this case) prior to adding 
it to the total.  I would assume that a significant amount of the overall 
process time can be attributed to the *retrieval* of the next value for 
summation.  In fact, if you write a TCL based loop to mimic the C version 
above, you'll see that it's at least an order of magnitude faster (at least 
in my tests), than your above TCL loop.

Now, that being said, you may very well want the summation loop to retrieve 
it's data from some other data structure.  I'm just pointing out that the 
above 2 examples aren't really the same - though I'm sure you already know 
that... ;^)

Jeff 



More information about the Metakit mailing list