| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| Thanks again, Harold. Any other takers on the original confusions? (Corrected version follows) > I wrote the C++ program at the end of this post to get an idea > of the relative speeds of various mathematical operations. Now > I'm wondering about two features of the results. Here's an > example output from an Athlon 800 machine: > >| Timing 2000 runs of 1000 calls >| >| div : 0.39 s 0.000195 ms/call >| mul : 0.38 s 0.00019 ms/call >| add : 0.39 s 0.000195 ms/call >| null: 0.33 s 0.000165 ms/call >| 1.2e+007 Repeat (y/n)? y >| div : 0.39 s 0.000195 ms/call >| mul : 0.38 s 0.00019 ms/call >| add : 0.38 s 0.00019 ms/call >| null: 0.88 s 0.00044 ms/call >| 1.38e+007 Repeat (y/n)? n > > Now-- > > 1) The "null" time is always the same as for add/mul on the > first run but about double that subsequently, as seen above. All > other numbers behave consistently. Why might that be? > > 2) I expected division to be rather slower than add/mul. > Sometimes the numbers are indeed higher but only by about 10% > and in a minority of runs. Am I observing throughput instead of latency, > which is what I'm interested in? I tried feeding > dummy (the local one) back into compute() but, range issues > aside, the times didn't change. --------------------------------- #include <iostream> #include <ostream> #include <iomanip> #include <vector> #include <cstdlib> #include <ctime> #include <cmath> using namespace std; const int KBlockSize = 1000, KRunCount = 2000; const int KCallCount = KBlockSize * KRunCount; void printClocks(const char* name, clock_t clocks) { float sec = float(clocks) / CLOCKS_PER_SEC; cout << right << setprecision(3); cout << name << ": " << setw(8) << sec << " s " << setw(8) << sec * 1000 / KCallCount << " ms/call\n"; } double dummy = 0; template<class Float, class Compute> class Runner { const char* name_; clock_t clocks_; public: Runner(const char* name) : name_(name) { vector<Float> v(KBlockSize + 1); for(int i = 0; i <= KBlockSize; ++i) v[i] = 1e-7 + rand() / Float(RAND_MAX); Compute compute; Float dummy = 0; clock_t clocks = -clock(); for(int r = 0; r < KRunCount; ++r) { for(int i = 0; i < KBlockSize; ++i) dummy += compute(v[i], v[i+1]); } clocks += clock(); ::dummy += dummy; clocks_ = clocks; } ~Runner() { printClocks(name_, clocks_); } }; struct Null { template<class Float> Float operator() (Float x, Float y) { return x; } }; struct Add { template<class Float> Float operator() (Float x, Float y) { return x + y; } }; struct Mul { template<class Float> Float operator() (Float x, Float y) { return x * y; } }; struct Div { template<class Float> Float operator() (Float x, Float y) { return x / y; } }; int main() { srand(time(0)); cout << "Timing " << KRunCount << " runs of " << KBlockSize << " calls\n\n"; char repeat; do { dummy = 0; { Runner<float, Null> r0("null"); Runner<float, Add > r1("add "); Runner<float, Mul > r2("mul "); Runner<float, Div > r3("div "); } cout << dummy << " Repeat (y/n)? "; } while(cin >> repeat && repeat != 'n'); return 0; } // end of code |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.