- Click to show recent forum activity -
Join our forums or log in.


What have you guys been working on?

What languages and projects have you guys been working on lately? I've mostly been doing web-related stuff (php/javascript/html/css) and some .NET. Played around with some Perl not too long ago and was thinking about get...

Project idea

Alright people ... let's start things up ... we have talented people around, we can do anything .. the thing i would like to do is a project that reaches alot of people and in different ways.. by that i mean web, Cross-p...

Should I add a hardware section?

Should I add a hardware section to the forums? I've been thinking about re-organizing the forums a bit and also seeing about getting some new moderators. Leave comments if you can and let me know. I'd like to get these f...

Getting started with .NET

If you didn't know, you can get a free version of Visual Studio 2008 -- Express Edition is what they call it. If you're interested in doing any kind of .NET programming I would highly recommend getting the free compiler ...

Related Posts

Spread the word

Digg this post

Bookmark to delicious

Stumble the post

Add to your technorati favourite

Subscribes to this post

5 users responded to this post

Nick said in February 8th, 2009 at 12:03 pm    

Nice test. Looking forward to seeing some more benchmarks.

Reply

akhila .p.shetty said in February 15th, 2009 at 9:12 am    

which statement is better if or switch in c programming ,taking into consideration of speed of execution of both statements.I also want to know about how if and switch statement works in veiw point of compiler.

Reply

Chris Farrar said in March 25th, 2009 at 11:21 am    

I am sorry to say, but your test appears flawed. I have been doing some profiling of switch statements and if else’s in my own work just to see what the exact difference was and have come up with wildly different results. I believe the problem with your test is that it is always using the same hard-coded value, which I believe the compiler is doing some optimizations in the switch test to remove the comparison entirely.

If you create a random table at the start, which is the size of the number of tests you would like to run and compare each indexed value down the if/else and through the switch you will see the if else out performs the switch by about 10% – 20% for highly varied values (between 0 and 80 for my test of 6 randomly chosen values).

If you create a second random table at the start and localize the spread of the values between a small set (I used 0 and 5) and test for each of the values ( 0 – 4 else/default == 5) you should notice there is vary little variance between the speed of the switch and if else (2% – 5% in my tests). I can post code if you would like, however I ran the test in an internal project of mine using some of my own timing code, so it may take a little creativity recreate the functionality(not that it is all that hard as you pointed out :) ).

I am anxious to see anyone else’s results, as I really have no preference in the matter, but would just like to know.

PS, I get the same variation between switch and if/else that you get using your code.

PPS, C++ not C, MSVC 8, however using the exact same code under Ubuntu w/ gcc 4 the difference grows.

Reply

Chris Farrar said in March 25th, 2009 at 12:49 pm    

^ update to my above post. There is only a difference in my test cases in a debug, unoptimized build. With optimizations turned on, there is little to no difference between the code segments, were talking less than 0.1%.. so, yeah, meh, user preference!

Reply

Adam said in March 11th, 2010 at 2:37 pm    

Are you aware of the buffer overflow in your main loop?

int main()

{

long ifs[100], sws[100];

ifs[100] = 0;

sws[100] = 0;

You allocated two arrays of 100 elements, indexed 0 through 99, and then you access and mutate element 100 in each array (and do so in further lines of code).

Reply

1 Pingback & Trackback On This Post
Leave Your Comments Below

Currently you have JavaScript disabled. In order to post comments, please make sure JavaScript and Cookies are enabled, and reload the page. Click here for instructions on how to enable JavaScript in your browser.