The functions were:
double ref(const double &d) { return static_cast<double>(rand())*d - 1000.0; }
double val(const double d) { return static_cast<double>(rand())*d - 1000.0; }
The function pointers were:
double (*fptr_ref)(const double &) = ref; double (*fptr_val)(const double) = val;
The input/output variables were:
double in = 34, out = 0;
The inner loop was:
QueryPerformanceCounter(&start_ref); for(size_t j = 0; j < 100000; j++) out = (*fptr_ref)(in); QueryPerformanceCounter(&end_ref);
The pass-by-reference version took on average about 0.0076 seconds per 100,000 calls, and the pass-by-value version took on average about 0.0072 seconds per 100,000 calls. In other words, I found that the pass-by-value version was roughly 1.06 times faster, which is to be expected.
For long unsigned int, the pass-by-reference version took on average about 0.00578 seconds per 100,000 calls, and the pass-by-value version took on average about 0.00573 seconds per 100,000 calls. The pass-by-value version was roughly 1.008 times faster, which is to be expected.
The functions were:
long unsigned int ref(const long unsigned int &d) { return rand()*d - 1000; }
long unsigned int val(const long unsigned int d) { return rand()*d - 1000; }
The function pointers and input/output types were unsigned long int.
Things changed for float though, and I cannot explain it to myself. The pass-by-reference version took on average about 0.00816159 seconds per 100,000 calls, and the pass-by-value version took on average about 0.00838039 seconds per 100,000 calls. The pass-by-reference version was roughly 1.03 times faster, which was unexpected. Of course, repeating the entire test yielded similar results every time. The standard deviation is about one order less than the average, which was also the case for double and long unsigned int, so it's not like all hell was breaking loose.
The functions were:
float ref(const float &d) { return static_cast<float>(rand())*d - 1000.0f; }
float val(const float d) { return static_cast<float>(rand())*d - 1000.0f; }
The function pointers and input/output types were float.
This was all on Windows 7 32-bit, using MSVC++ 2010 Express in release mode.
Pass-by-reference was generally superior for composite types, which is to be expected.
Is there any obvious reason as to 1) why float would generally take longer than double, and 2) why pass-by-reference would be faster than pass-by-value for float?
Edited by taby, 18 June 2012 - 09:42 PM.






