| | |
Resetting the clock()
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
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 :
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.
C++ Syntax (Toggle Plain Text)
//Project: Mod_Efficiency //Author : Kyle Wesley //Descrip: This was made to prove that mod/div operations are often quite inefficient compared to their more basic // counterparts... #include<ctime> #include<iostream> using namespace std; void DoEfficiencyDebug(); int main() { int ROW(100); int iX(0); //coordinate x int iY(0); //coordinate y cout << "Doing 10000000 mod + divide calculations..."; for (int i = 1; i < 10000000; i++) { iX = i % ROW; iY = i / ROW; } //clock(); /*cout << "Doing 10000000 non mod calculations..."; for (int i = 1; i < 10000000; i++) { iX++; if (iX >= ROW) { iY++; iX = 0; } } */ DoEfficiencyDebug(); cin.get(); return 0; } void DoEfficiencyDebug() { cout << "\n\nClock ticks for calculation: " << clock(); cout << "\n@ " << CLOCKS_PER_SEC << " For Duration: " << clock() / (CLOCKS_PER_SEC / 1000) << " milliseconds"; return; }
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
Why you want to reset system clock? Keep it simpler:
So your original timing was incorrect (from the PROGRAM START to the current moment).
C++ Syntax (Toggle Plain Text)
clock_t t0, t1; t0 = clock(); // do this... t0 = clock() - t0; t1 = clock(); // do that... t1 = clock() - t1; // Now print t0, t1 and what else...
>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:
Has identical results to this:
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:
C++ Syntax (Toggle Plain Text)
iX = i % ROW; iY = i / ROW;
C++ Syntax (Toggle Plain Text)
iX++; if (iX >= ROW) { iY++; iX = 0; }
I'm here to prove you wrong.
•
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
•
•
•
•
>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:
Has identical results to this:C++ Syntax (Toggle Plain Text)
iX = i % ROW; iY = i / ROW;
C++ Syntax (Toggle Plain Text)
iX++; if (iX >= ROW) { iY++; iX = 0; }
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.
•
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
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.
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.
C++ Syntax (Toggle Plain Text)
//Project: Mod_Efficiency //Author : Kyle Wesley //Descrip: This was made to prove that mod/div operations are often quite inefficient compared to their more basic // counterparts... #include<ctime> #include<iostream> using namespace std; int DoEfficiencyDebug(int = 0); int const MAX(100); int const ROW(10); int main() { int iTimeOffset(0); int iX(0); //coordinate x int iY(0); //coordinate y double dEff(0); cout << "Row Max: " << ROW << endl; cout << "Doing " << MAX << " div + mod calculations..."; for (int i = 1; i < MAX; i++) { iX = i % ROW; iY = i / ROW; cout << "\niX: " << iX << " iY: " << iY; } iX = 0; iY = 0; iTimeOffset = DoEfficiencyDebug(); cout << "\n\nDoing " << MAX << " non div + mod calculations..."; for (int i = 1; i < MAX; i++) { iX++; if (iX >= ROW) { iY++; iX = 0; } cout << "\niX: " << iX << " iY: " << iY; } cout << "\niX: " << iX << " iY: " << iY; dEff = static_cast<double>(iTimeOffset) / static_cast<double>(DoEfficiencyDebug(iTimeOffset)); cout << "\n\nNon Div Mod Efficiency: " << dEff * 100 << "% more efficient than Div Mod"; cin.get(); return 0; } int DoEfficiencyDebug(int iOffset) { cout << "\n\nClock ticks for calculation: " << clock() - iOffset; cout << "\n@ " << CLOCKS_PER_SEC << " For Duration: " << (clock()-iOffset) / (CLOCKS_PER_SEC / 1000) << " milliseconds"; return (clock() - iOffset); }
Last edited by skatamatic; Sep 8th, 2008 at 8:43 pm. Reason: added code type
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.
>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:
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
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:
C++ Syntax (Toggle Plain Text)
int iX = i % ROW; int iY = i / ROW;
C++ Syntax (Toggle Plain Text)
int iX = 0; int iY = 0; for ( int j = 0; j < i; j++ ) { if ( ++iX >= ROW ) { ++iY; iX = 0; } }
>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.
•
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
•
•
•
•
>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:
C++ Syntax (Toggle Plain Text)
int iX = i % ROW; int iY = i / ROW;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.C++ Syntax (Toggle Plain Text)
int iX = 0; int iY = 0; for ( int j = 0; j < i; j++ ) { if ( ++iX >= ROW ) { ++iY; iX = 0; } }
>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 whateverclock() - 0produces. 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.
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. >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.
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.
![]() |
Similar Threads
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- Machine restarts on its own (Troubleshooting Dead Machines)
- HJT log please check, causing serious boot probs (Viruses, Spyware and other Nasties)
- Setting Timer/Clock Precision (Visual Basic 4 / 5 / 6)
- xp starts up pc every day (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: Little Help?
- Next Thread: random array
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






