Resetting the clock()

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Resetting the clock()

 
0
  #1
Sep 8th, 2008
Ok, so I'm trying to prove to my teacher that Div and Mod isn't always the best way to solve things. So i created this tiny program, held entirely in main.cpp :

  1. //Project: Mod_Efficiency
  2. //Author : Kyle Wesley
  3. //Descrip: This was made to prove that mod/div operations are often quite inefficient compared to their more basic
  4. // counterparts...
  5.  
  6. #include<ctime>
  7. #include<iostream>
  8. using namespace std;
  9.  
  10. void DoEfficiencyDebug();
  11.  
  12. int main()
  13. {
  14. int ROW(100);
  15. int iX(0); //coordinate x
  16. int iY(0); //coordinate y
  17. cout << "Doing 10000000 mod + divide calculations...";
  18. for (int i = 1; i < 10000000; i++)
  19. {
  20. iX = i % ROW;
  21. iY = i / ROW;
  22. }
  23. //clock();
  24. /*cout << "Doing 10000000 non mod calculations...";
  25. for (int i = 1; i < 10000000; i++)
  26. {
  27. iX++;
  28. if (iX >= ROW)
  29. {
  30. iY++;
  31. iX = 0;
  32. }
  33. }
  34. */
  35. DoEfficiencyDebug();
  36.  
  37. cin.get();
  38. return 0;
  39. }
  40.  
  41.  
  42. void DoEfficiencyDebug()
  43. {
  44. cout << "\n\nClock ticks for calculation: " << clock();
  45. cout << "\n@ " << CLOCKS_PER_SEC << " For Duration: " << clock() / (CLOCKS_PER_SEC / 1000) << " milliseconds";
  46. return;
  47. }

The div/mod code and the basic iteration both do essentially the same thing (in the program we had to make) which is to plot squares appropriately on a grid. My only issue is that i'm not sure how to reset the clock(), so it has to be run in 2 different instances - one with mod/div commented out, one with it commented in. Does anyone know a reset clock() method?

For anyone who's interested, here's the results from this program (in terms of efficiency):

For 10000000 Calculations:
Mod/Div method : 1492 clock ticks or about 1500 milliseconds
Basic Iteration : 160 clock ticks or about 165 milliseconds

You're all welcome to show your instructors this =) . Although mod/div is occassionally neccessary.
Last edited by skatamatic; Sep 8th, 2008 at 2:44 pm. Reason: Updated to make more sense
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Resetting the clock()

 
0
  #2
Sep 8th, 2008
Why you want to reset system clock? Keep it simpler:
  1. clock_t t0, t1;
  2. t0 = clock();
  3. // do this...
  4. t0 = clock() - t0;
  5. t1 = clock();
  6. // do that...
  7. t1 = clock() - t1;
  8. // Now print t0, t1 and what else...
So your original timing was incorrect (from the PROGRAM START to the current moment).
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,652
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 722
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Resetting the clock()

 
1
  #3
Sep 8th, 2008
>I'm trying to prove to my teacher that Div and Mod isn't always the best way to solve things.
I find it humorous that you seem to think "fastest" and "best" are synonymous.

>You're all welcome to show your instructors this =)
Oh, I will indeed. It's a very amusing test, seeing as how an accurate comparison of a feature and it's alternative should produce the same results for both when hoisted out of the timing loop. I'm curious how you think that this:
  1. iX = i % ROW;
  2. iY = i / ROW;
Has identical results to this:
  1. iX++;
  2. if (iX >= ROW)
  3. {
  4. iY++;
  5. iX = 0;
  6. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: Resetting the clock()

 
0
  #4
Sep 8th, 2008
Originally Posted by Narue View Post
>I'm trying to prove to my teacher that Div and Mod isn't always the best way to solve things.
I find it humorous that you seem to think "fastest" and "best" are synonymous.

>You're all welcome to show your instructors this =)
Oh, I will indeed. It's a very amusing test, seeing as how an accurate comparison of a feature and it's alternative should produce the same results for both when hoisted out of the timing loop. I'm curious how you think that this:
  1. iX = i % ROW;
  2. iY = i / ROW;
Has identical results to this:
  1. iX++;
  2. if (iX >= ROW)
  3. {
  4. iY++;
  5. iX = 0;
  6. }
Well. I'm sorry if a 1300% increase in speed isn't considered better in your opinion, you may be thinking in terms of robustness and end user usability. This is using the same logic as the fastest runner is the best runner in the race, which I hope you can draw a logical connection with. My argument is based entirely on efficiency - the number of calculations required to get the same end result. And sorry, replace iX and iY with iY and iX in the first loop and you will find that they are indeed identical results.

And as for the above solution to my problem, that is indeed the 'fix' i used. But my question is if there are any functions in the ctime library that would reset the clock() variable. I may end up just looking through the microsoft documentation, but I think we all know how painful that can be.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: Resetting the clock()

 
0
  #5
Sep 8th, 2008
For Narue: This is the latest code for the efficiency test, and you will find the results to be quite identical. I'm sorry for the mixup of the two variables earlier, but I was pretty tired and/or stoned when I originally wrote this.

Note: The efficiency gain in my method decreases to around 300% at lower iterations. But since i've included cout lines to prove the identicality, 100000000 iterations isn't feasable. To fix this take out the couts and increase MAX to 100000000, and row to whatever you want, really.

  1. //Project: Mod_Efficiency
  2. //Author : Kyle Wesley
  3. //Descrip: This was made to prove that mod/div operations are often quite inefficient compared to their more basic
  4. // counterparts...
  5.  
  6. #include<ctime>
  7. #include<iostream>
  8. using namespace std;
  9.  
  10. int DoEfficiencyDebug(int = 0);
  11. int const MAX(100);
  12. int const ROW(10);
  13.  
  14. int main()
  15. {
  16. int iTimeOffset(0);
  17. int iX(0); //coordinate x
  18. int iY(0); //coordinate y
  19. double dEff(0);
  20. cout << "Row Max: " << ROW << endl;
  21. cout << "Doing " << MAX << " div + mod calculations...";
  22. for (int i = 1; i < MAX; i++)
  23. {
  24. iX = i % ROW;
  25. iY = i / ROW;
  26. cout << "\niX: " << iX << " iY: " << iY;
  27. }
  28. iX = 0;
  29. iY = 0;
  30. iTimeOffset = DoEfficiencyDebug();
  31. cout << "\n\nDoing " << MAX << " non div + mod calculations...";
  32. for (int i = 1; i < MAX; i++)
  33. {
  34. iX++;
  35. if (iX >= ROW)
  36. {
  37. iY++;
  38. iX = 0;
  39. }
  40. cout << "\niX: " << iX << " iY: " << iY;
  41. }
  42. cout << "\niX: " << iX << " iY: " << iY;
  43. dEff = static_cast<double>(iTimeOffset) / static_cast<double>(DoEfficiencyDebug(iTimeOffset));
  44. cout << "\n\nNon Div Mod Efficiency: " << dEff * 100 << "% more efficient than Div Mod";
  45. cin.get();
  46. return 0;
  47. }
  48.  
  49.  
  50. int DoEfficiencyDebug(int iOffset)
  51. {
  52. cout << "\n\nClock ticks for calculation: " << clock() - iOffset;
  53. cout << "\n@ " << CLOCKS_PER_SEC << " For Duration: " << (clock()-iOffset) / (CLOCKS_PER_SEC / 1000) << " milliseconds";
  54. return (clock() - iOffset);
  55. }
Last edited by skatamatic; Sep 8th, 2008 at 8:43 pm. Reason: added code type
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,407
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1467
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Resetting the clock()

 
0
  #6
Sep 8th, 2008
The win32 api function you want is SetSystemTime()
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: Resetting the clock()

 
0
  #7
Sep 8th, 2008
Originally Posted by Ancient Dragon View Post
The win32 api function you want is SetSystemTime()
Thank you very much =).
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,652
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 722
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Resetting the clock()

 
1
  #8
Sep 9th, 2008
>I'm sorry if a 1300% increase in speed isn't considered better in your opinion
Certainly not in this case. You failed to describe exactly what you meant by saying "best" (assuming that "best" was execution speed), you failed to compare two equivalent solutions to prove your assertion, and you failed to compare the execution speeds correctly. Therefore your premise was subjective, your test was flawed, and your conclusion was biased.

Show me a legitimate 1300% increase in speed and I'll take you more seriously.

>This is the latest code for the efficiency test, and you will find the results to be quite identical.
Not when taken out of the timing loop. The timing loop should be only for timing, but your "non div + mod calculations" require it for correct behavior. These two snippets are equivalent without the timing loop:
  1. int iX = i % ROW;
  2. int iY = i / ROW;
  1. int iX = 0;
  2. int iY = 0;
  3.  
  4. for ( int j = 0; j < i; j++ ) {
  5. if ( ++iX >= ROW ) {
  6. ++iY;
  7. iX = 0;
  8. }
  9. }
That's what I mean by identical results. If you remove the timing loop and run the code with any value of i, and the result is the same. In your code the result is only the same when run with consecutive values of i, which means your method is dependent on the timing loop while the method you claim to be flawed is not.

>The efficiency gain in my method decreases to around 300% at lower iterations.
The efficiency gain, aside from being biased in that your method being tested is incomplete, is also biased by broken code. You're careful to set the time offset for your method, but for the "slow" method you don't bother and just go with whatever clock() - 0 produces. Your timing code is broken and it's conveniently broken in favor of your method. This is easily proven by swapping the two loop bodies such that your method is tested first. You'll likely find the "1300% increase" to no longer be in your favor.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: Resetting the clock()

 
0
  #9
Sep 9th, 2008
Originally Posted by Narue View Post
>I'm sorry if a 1300% increase in speed isn't considered better in your opinion
Certainly not in this case. You failed to describe exactly what you meant by saying "best" (assuming that "best" was execution speed), you failed to compare two equivalent solutions to prove your assertion, and you failed to compare the execution speeds correctly. Therefore your premise was subjective, your test was flawed, and your conclusion was biased.

Show me a legitimate 1300% increase in speed and I'll take you more seriously.

>This is the latest code for the efficiency test, and you will find the results to be quite identical.
Not when taken out of the timing loop. The timing loop should be only for timing, but your "non div + mod calculations" require it for correct behavior. These two snippets are equivalent without the timing loop:
  1. int iX = i % ROW;
  2. int iY = i / ROW;
  1. int iX = 0;
  2. int iY = 0;
  3.  
  4. for ( int j = 0; j < i; j++ ) {
  5. if ( ++iX >= ROW ) {
  6. ++iY;
  7. iX = 0;
  8. }
  9. }
That's what I mean by identical results. If you remove the timing loop and run the code with any value of i, and the result is the same. In your code the result is only the same when run with consecutive values of i, which means your method is dependent on the timing loop while the method you claim to be flawed is not.

>The efficiency gain in my method decreases to around 300% at lower iterations.
The efficiency gain, aside from being biased in that your method being tested is incomplete, is also biased by broken code. You're careful to set the time offset for your method, but for the "slow" method you don't bother and just go with whatever clock() - 0 produces. Your timing code is broken and it's conveniently broken in favor of your method. This is easily proven by swapping the two loop bodies such that your method is tested first. You'll likely find the "1300% increase" to no longer be in your favor.
Ok I swapped them. Your right to a very small degree, in that the initializations and other such shinnanigans do take a very small amount of time. I timed them, actually, and they require about 11 clock ticks (which is entirely insignificant when compared to the thousands required in the end, but still duly noted). But its still about 1000% faster. And I wasn't trying to totally knock off div/mod. If this function were to find the exact location of a given entry in the table then div mod would be the way to go. However, this is merely for building a table, and is a demonstration on how using the supposed 'correct' way can often be surpassed by simpler solutions. The amount of calculations performed at the lowest level by a div/mod is ludicrous for something like this, especially in the scale presented (billions, or even trillions of iterations). Each mod requires a loop in itself, and as MAX increases, the time taken for this loop increases, thus leading to an exponential growth of inefficiency.

While the robustness of the function is hampered by my method (since it does require the timing loop), this particular application of these algorithms definetly does not need the robust capabilities of div/mod. In fact, it's the div/mod calculations that bottleneck the graphical drawer we are using, which is very bad! The div/mod method causes noticable delay between drawings, while mine builds the table (nearly) instantly. I suppose a more in depth explanation could have been due earlier, but I didn't expect my code to face such scrutiny, rather I was searching for an answer to a fairly unrelated problem. But I suppose it's appreciated . Cheers.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,652
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 722
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Resetting the clock()

 
1
  #10
Sep 9th, 2008
>Your right to a very small degree
That's a pretty arrogant statement, all things considered.

>But its still about 1000% faster.
Not on my system. I show a complete reversal of the timings, which means your code is also non-portable. Fancy that.

>I suppose a more in depth explanation could have been due earlier
Too late, you fail.

>but I didn't expect my code to face such scrutiny
You were planning on using that code essentially to prove that your instructor is incompetent. I have trouble seeing how not only you didn't expect close scrutiny by someone, but that you were also so sloppy as to completely fail such scrutiny both in describing the purpose of the code and writing the code itself.

>But I suppose it's appreciated
Bite me. Nothing irritates me more than people dismissing my contributions with a smarmy comment. Don't expect any help from me in the future.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC