
Just to make the case for fixed-point accuracy over flixed-point speed: template <class Number> Number work(size_t iterations, std::vector<Number> const &data) { Number sum = 0; size_t size = data.size(); for (size_t i = 0; i < iterations; ++i) { Number a = data[i % size]; Number b = data[(i + 500)%size]; sum += a * b; } return sum; } BOOST_AUTO_TEST_CASE(test_floats) { size_t iterations = 1000*1000*100; size_t outter = 1;//00;//0*1000*10; double int_t = 0; double float_t = 0; size_t size = 100000; std::vector<int> ints(size); srand(42); generate_n(ints.begin(), size, rand); std::vector<float> floats(size); srand(42); generate_n(floats.begin(), size, rand); boost::timer int_timer; int int_sum = 0; for (size_t n = 0; n < outter; ++n) { int_sum += work(iterations, ints); } int_t = int_timer.elapsed(); boost::timer float_timer; float float_sum = 0; for (size_t n = 0; n < outter; ++n) { float_sum += work(iterations, floats); } float_t = float_timer.elapsed(); cout << int_t << ", " << float_t << "; " << int_sum << ", " << float_sum << endl; }
1.103, 0.949; 129244096, 1.80144e+016 In this (slightly-less non-toy case), floats were a little bit faster. If you actually used fixed-point here, rather than floats, then the difference would be much larger again. Regards, Christian On Fri, Jun 26, 2009 at 12:23 PM, Christian Schladetsch < christian.schladetsch@gmail.com> wrote:
I just wrote a quick test with the following loop for some random initialization values:
for (int i = 0; i < iterations; ++i) { sum += a * b; a += b; }
I used MSVC++ to compile.
I set iterations to a billion. When I used ints I got around 800 ticks. When I used floats I got around 3700 ticks.
That is almost a factor five. For my purposes, that's an optimization I am willing to sacrifice a lot for.
I just did the same test, with SSE2 enabled.
int: 0.916 seconds, 1321730048 float: 1.398 seconds, 4.5036e+015
In this case, floats were 70% the speed of ints, but also gave a modicum of the correct result.
The ints were faster but gave the incorrect result. Furthermore, this was ints not fixed-point. Would doing it using an actual fixed-point number make up the %30 difference? Perhaps, but it would still give the wrong result.
I didn't mean to claim that fixed-point was "always slower than floating point" - especially for toy cases like this ints will be faster (if wrong) because there is nothing else for the CPU todo while the FPU is working.
Add in some fetches or stores and I think you will get a far different result.
In any case, I merely intended to make the point that one good reason for using fixed-point over floating point is accuracy and not necessarily speed.
Regards, Christian.