Array Speed comparison in STL/C++, C and Java

May 25, 2006

I originally meat to post this in someone else’s blog, but I decided not to. He claims that he get a speed boost from converting his program that were using STL/C++ to array characters in C. And furthermore claims that in JavaScript he gets even more performance, which I can never believe ever myself.

My reply:

i never compared the code performance between c, stl/c++ and java itself, but looking into the way you put it, i have to disagree with it. STL/C++ might be a bit slower than the C traditional array. but it actually depends on the STL implementation. but lets not talk about the implementation right now, one way to boost your performance is to prepare the stl string object before you use it. remember that string objects doesnt have limits the way traditional array have. how come they achive this? in actuality, strings shrinks and expands (well actually they expand automatically, and you have to manually shrink it via a method). so, here’s what a common behavior stl string is. a string were given an initial size. everytime that string reaches the limit of the size, an internal kicks in that expand the string by allocating a new memory space twice the size of the current string and then copying all the data there + the data that weren’t fit before. so, stl strings size doubles each time it reaches it limits. as we can see, the act of reallocating memory and moving the data to new location requires quite a bit of time. imagine if you are doing it with a string which initial size is of 2, and you need to insert 1024 bytes of data. you’ll need 10 times of realocation and memory moves to achieve this. so, to improve the performance of stl string, first set the initial size to the number of data that you’ll be needing.

now lets take a look into the assembly of array allocation in c. it actually might look like like this:

variable1 db 1024

and to put a number in it (99)

lea si, variable1 // or mov si, offset variable1
mov si, 99

we have just insert 99 to the first address memory of variable1. to create a loop and insert 1024 99s:

mov bx, offset variable1
mov cx, 1024
mov [bx], BYTE PTR 0
loop the_loop
you dont have to worry about the details, this assembly is in 8086 and if we expand the code and the loop, well get around 1024 + 2 lines of assembly codes to fill up the complete array. that means 1026 linear single pipe instruction code cycle. (in a simple way to put it, 1024 cpu clock out of nGHz you have in your computer right now).

why do i put that assembly explanation up there? it is to show you that nothing beats that! the c code should produce not so far result from the assembly code i described above. however with stl, we handles functions and objects. which of course requires overheads when calling their methods plus the internal checkings and administration that were performed. however, while stl does have its own overhead, it still compiles directly to machine code which should be pretty efficient. the worst should be java. Java requires a Virtual Java Machine layer installed. Think of it like a machine that runs inside your machine. the java code will run in this ‘virtual machine’ and the internal function of this ‘virtual machine’ will translate java binary codes as ‘efficient’ as possible to your machine code, thus even produced more overhead.


One Response to “Array Speed comparison in STL/C++, C and Java”

  1. manly Says:

    Very Very nice information here… Thanks

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: