Re: many Array creations are slowing down

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: David Wang [Msft] (someone_at_online.microsoft.com)
Date: 02/22/04


Date: Sun, 22 Feb 2004 00:50:57 -0800

What is killing performance in this case is NOT Array creation but rather
the a.push() command.
- If you remove the a.push(cp); instruction, you should see relatively
steady execution timing.
- If you change it to a.push(1); instruction, you see steadily increasing
timing
- If you change it to a[i] = 1; instruction, you see relatively steady
execution timing
- If you change it to a[i] = cp; instruction, you see relatively steady
execution timing

This suggests that a.push() of dynamic objects isn't performant (not that
hard to believe). If you are truly performance conscious, I would suggest
you:
1. Preallocate the Array's size and avoid the costs of "growing" the array
during execution, which would randomize performance
2. Directly index into the array to avoid other unnecessary calculations

Honestly, asking why Mozilla/Netscape/Opera allows a certain snippet of
unperformant code to execute faster than something else is not really
meaningful because I can just say "it's featuritis" and point you at another
snippet that Mozilla/Netscape/Opera will execute slower. The way to have
consistent performance is to write performant code, which then allows one to
legitimately ask why it's slower in particular implementations.

-- 
//David
IIS
This posting is provided "AS IS" with no warranties, and confers no rights.
//
"Alexander LAW" <test@mail.ru> wrote in message
news:%23o2fvfE%23DHA.3496@TK2MSFTNGP10.phx.gbl...
Hello, can anyone tell me why creation of Array is slowing down?
Why second thousand of "new Array()" executes slower than the first one?
Here is the small test:
<HTML>
<BODY>
<SCRIPT>
  var a=new Array(), dt1, dt2, dtcnt, s="";
  for (var j=0;j<10;j++) {
    dt2=0; dtcnt=0;
    for (var i=0;i<10000;i++) {
dt1=(new Date()).valueOf();
      cp=new Array();
dt2=dt2+((new Date()).valueOf()-dt1); dtcnt++;
      a.push(cp);
    }
    s=s+j+": "+dt2/1000+"\n";
  }
  alert(s);
</SCRIPT>
On Athlon-1200 I get:
0: 0.421
1: 1.122
2: 1.732
3: 2.384
4: 3.046
5: 3.617
6: 4.274
7: 4.858
8: 5.518
9: 6.151
Mozilla, Netscape, Opera are executing this much faster.
Mozilla, for example:
0: 0.2
1: 0.21
2: 0.211
3: 0.13
4: 0.16
5: 0.18
6: 0.2
7: 0.201
8: 0.16
9: 0.2
I think, there is some oddity in scripting host core...
Maybe someone knows workarounds of this?
Thanks in advance.
WBR,
Alexander
</BODY>
</HTML>


Relevant Pages

  • Re: Why does this work? POSTPONE [Beginner]
    ... my-dup ACCESS_VIOLATION ... for example, into a CALL instruction. ... ' FOO was the index of the corresponding entry in that array. ... had a strange system of associations: token of love + execution ...
    (comp.lang.forth)
  • Re: Postscript questions
    ... It looks as if you believe the curly braces are defining a scope. ... Curly braces are behaving much more like the square brackets, the array "delimiters". ... The difference is deferred execution, ... Evaluating an executable name means that the interpreter searchs the name through the dictionnary stack and when found, evaluate the value associated with the name. ...
    (comp.lang.postscript)
  • Re: Postscript questions
    ... It looks as if you believe the curly braces are defining a scope ... The difference is deferred execution (and ... A procedure is an executable array. ... Evaluating an executable name means that the interpreter searchs ...
    (comp.lang.postscript)
  • Re: Re: Re: RE: Perl Question: Optimization
    ... @ids array changes, the statement gets re-prepared, so unless you have the ... # THIS INPUT DATA FILE CONTAINS ABOUT 40000 ROWS ... >prepared statement inside the loop of 40000. ...
    (perl.dbi.users)
  • Re: Loading EXE files directly from memory?
    ... second, you must consider the different executable file formats, ELF ... entire executable file in an array, you can put only the code that you ... 2- modify the execution flow of the program to the array. ... if the array code is encripted, in a remote server, is ...
    (Pen-Test)