Back of the envelope: Remember! There is a compiler.

Thx to my adviser I realized that there is a compiler translating the stuff that I code.  This becomes extremely relative when you are trying to time certain things.  Is the compiler really translating the code that I write to an executable that does exactly the same thing that I told it to do? Or does it think it’s better than me? :)

When I looked at Philippe’s (My adviser) java code, I realized that he had used a static variable outside the functions as the destination of the operation.  When I asked him for the reason of his decision, he answered: “If you were a compiler, and you saw a loop get executed many times to do exactly the same and the results is thrown away. What would you do?”.  I immediately realized my potential mistake and went back to the code :).  I now have two different files that I have analyzed.  One is the previous C file with some changes.  And the other is a modification of the java file that Philippe gave me [1],[2].

The following are my results when I run the commands in my machine.  I know little about what the compiler does in these cases.  With that said, the output of the commands is very…. interesting :)

The result from the C code:

[joel@jogr temp]$ gcc temp.c -O0 -o operations
[joel@jogr temp]$ ./operations 1000000000
Global variable
Add: 1890.000000 ms
Mul: 3030.000000 ms
Div: 6880.000000 ms
Local variable
Add: 2110.000000 ms
Mul: 3040.000000 ms
Div: 6900.000000 ms
[joel@jogr temp]$ gcc temp.c -O2 -o operations
[joel@jogr temp]$ ./operations 1000000000
Global variable
Add: 0.000000 ms
Mul: 540.000000 ms
Div: 4230.000000 ms
Local variable
Add: 0.000000 ms
Mul: 0.000000 ms
Div: 0.000000 ms

These results differ from the ones I posted in my first “Back of the envelope” post.  Notice that I not only changed the code to use a global var, but I also used to different compilation options.  We see here that gcc (the compiler that I used) thinks it’s really smart when used with -O2.  When it sees that I used a local variable in my for loops, it just ignored it completely.  When using a global variable it “thinks twice” about optimizing the for loop completely out and chooses something else.  Though the use of a global variable seems to be faster.

The result from the Java code:

[joel@jogr temp]$ javac C.java
[joel@jogr temp]$ java C  1000000000
Global vars:
Add: 750 ms
Mul: 1688 ms
Div: 2317 ms
Local vars
Add: 745 ms
Mul: 746 ms
Div: 744 ms
[joel@jogr temp]$

I didn’t find any optimizing option that I could use with javac, so I chose to just run it one time.  Here we see clearly that my java compiler thinks it’s really smart too.  When it sees that I’m using local variables it is faster than when I use a global variable.  This probably means that it is somehow optimizing stuff under the hood.

It is curious (to me) how much faster java is.  I confess that I don’t know lots of things that happen in the java and C compilation processes.  But I still would have thunk that, out of these two files, C would have come out on top.  I guess you never stop surprising yourself :)  That’ll teach me to pick on java :)

Would love to hear what happened when you tried it!!! Leave a comment with your results or if you see that I did something completely idiotic :)

[1] http://www.itu.dk/people/jogr/classes/SDBS/FALL2010/20100908operations.c
[2] http://www.itu.dk/people/jogr/classes/SDBS/FALL2010/20100908C.java
Advertisements

About joelgranados

I'm fascinated with how technology and science impact our reality and am drawn to leverage them in order to increase the potential of human activity.
This entry was posted in PhD. Bookmark the permalink.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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