Help - Loop time?

Just started with C++. Currently taking a Fundamentals class. How long should it take a 2.66Ghz Pentium 4 with 1G of ram to count in a loop to 100,000,000? I have to write a program that will sum fractions of 1/x from 1 to 100,000,000 and from 100,000,000 to 1 as float and double and display the results. I put together a loop but it took about 4 hours to get to 17,000,000. Any ideas? I am using Visual Studio 2010. Here is what I have so far... It works with small loop values but the answers are all the same. At large loop values there is supposed to be a difference. My problem is that I don't want to wait for days to get an answer. I have it set to display the counter value on the screen. Would that slow it down?


Last edited on
Displaying has almost no effect on an algorithms efficiency. Your code is almost unreadable, with the lack of tags and variable naming, and no comments. Just taking some guesses here, but I think your problem may lay in using floating point in places you dont need to. Do f1 and d1 really need to be floating point? Looks like they are just being used as an int anyway. I'd not use floating point if you dont need to, calculations on them are a lot slower than just using an int. You can also speed things up a little bit by using unsigned. If you put some code tags and actually make this a little more readable, we can help you some more. But, I'd try my suggestions first
This program spends almost all of its time flushing the standard output, which is what endl does.

To speed it up
1) replace endl with \n
2) don't output on every iteration (output every 1000th or so)
3) don't output from the loop at all.
4) compile in Release (as opposed to Debug) mode.
Thanks Cubbi / Biscuit. Now it works in about 5 seconds. The biggest change from eliminating output from inside the loop.


Last edited on
Also looks like you can just get rid of either f1 or d1. They both do the same thing, unless I'm missing something
Topic archived. No new replies allowed.