performance cost of sample conversion

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Sep 6 22:45:58 PDT 2017


On 09/06/2017 07:06 PM, Psychological Cleanup wrote:
> if I have a non-double buffer and temporarily convert to double then
> convert back, do I save many cycles rather than just using a double
> buffer? I know it will bea lot more memory, but I'm specifically talking
> about the cycles in converting to and from vs no conversion.
>
> Using a double for everything gives the highest precision and makes
> things much easier but is that the way to go or does it costs quite a
> bit in performance?

You have to measure. Here's a start:

import std.conv;
import std.range;
import std.datetime;
import std.stdio;

double workWithDouble(double d) {
     return d * d / 7;
}

void workWithFloats(float[] floats) {
     foreach (ref f; floats) {
         f = workWithDouble(f).to!float;
     }
}

void workWithDoubles(double[] doubles) {
     foreach (ref d; doubles) {
         d = workWithDouble(d);
     }
}

void main() {
     foreach (n; [ 1_000, 1_000_000, 10_000_000 ]) {
         const beg = -1f;
         const end = 1f;
         const step = (end - beg) / n;
         auto floats = iota(beg, end, step).array;
         auto doubles = iota(double(beg), end, step).array;
         {
             auto sw = StopWatch(AutoStart.yes);
             workWithDoubles(doubles);
             writefln("%10s no   conversion: %10s usecs", n, 
sw.peek().usecs);
         }
         {
             auto sw = StopWatch(AutoStart.yes);
             workWithFloats(floats);
             writefln("%10s with conversion: %10s usecs", n, 
sw.peek().usecs);
         }
     }
}

Conversion seems to be more costly:

       1000 no   conversion:         27 usecs
       1000 with conversion:         40 usecs
    1000000 no   conversion:       1715 usecs
    1000000 with conversion:       5412 usecs
   10000000 no   conversion:      16280 usecs
   10000000 with conversion:      47190 usecs

Ali



More information about the Digitalmars-d-learn mailing list