If you have the data in an array, does the Tcl code

   set sum 0
   for {set i 0} {$i < 49999} {incr i} {
       incr sum $data($i)
   }

run faster? That would have been *my* obvious way of writing it. I get:

% set sum 0
0
% time {for {set i 0} {$i < 49999} {incr i} {incr sum $data($i)}} 10
479583 microseconds per iteration

If you have the data in a list and not an array, then

   set sum 0
   for {set i 0} {$i < 49999} {incr i} {
       incr sum [lindex $data $i]
   }

I get:

% time {for {set i 0} {$i < 49999} {incr i} {incr sum [lindex $data $i]}} 10
198283 microseconds per iteration

No idea why lists are 2.5 times faster than arrays... :)

--JYL

> To all language specialists: I'm looking for a way to establish some
> basic performance figures, to compare and evaluate a number of
> approaches I'm exploring in the Vlerq project.
>
> As a very first datapoint, it would be nice to find out how one
> writes decent loops for a very simple task: sum the items of a list
> of 50,000 integers, running from 0 to 49,999.  This is quite an
> important operation in MK, where cumulative offsets must often be
> calculated - it also gives an indication how efficient integer lists/
> vectors are.
>
> 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 }
>
> My question is: how would you write the above in <insert your
> language of choice here> ?
>
> This is not flame bait.  I'm not trying to prove X is better than Y,
> I'm trying to find out what range of performances one sees these
> days, and how much I can get away with for now by *not* optimizing my
> new code to the limit (it also affects some major decisions on what
> internal data structures I should use at this stage).
>
> I'm aware of the various "language shootout" websites, the risks of
> benchmarking, and cache effects.  Still, self-contained examples of
> this logic would help me avoid seriously flawed timings in other
> languages when applied to tasks which are relevant to Metakit.
>
> I'll summarize results.
>
> -jcw
>
> PS.  All timing comparisons are being done using a PIII/650 on
> Linux.  I've got the following installed so far if you're interested:
> python 2.3.4, perl 5.8.5, ruby 1.8.2, php 4.3.10, java 1.4.2, icon
> 9.40, gforth 0.6.2, lua 5.0.2 - can add more as needed.
> _____________________________________________
> Metakit mailing list  -  [email protected]
> http://www.equi4.com/mailman/listinfo/metakit
>


_____________________________________________
Metakit mailing list  -  [email protected]
http://www.equi4.com/mailman/listinfo/metakit

Reply via email to