Let's say I want to create an array of random numbers and do some operations on them:

void main() {

    import std.random;

    //Generate array of random numbers
    int arrSize = 100000000;
    double[] arr = new double[](arrSize);
    foreach (i; 0..arrSize)
        arr[i] = uniform01();

    //Call funcA on array elements
    foreach (i; 1..arr.length-1)
        funcA(arr,i);
}

void funcA(double[] arr, size_t i) {
    arr[i+1] = arr[i-1]+arr[i];
    funcB(arr,i);
}

void funcB(double[] arr, size_t i) {
    arr[i-1]= arr[i] + arr[i+1];
    arr[i] = arr[i-1] + arr[i+1];
    arr[i+1]= arr[i-1] + arr[i];
}

Now I dmd -profile it and look at the performance of funcA with d-profile-viewer. Inside funcA, only 20% of time is spend in funcB, but the rest 80% is self-time of funcA. How is it possible, when funcB has three times the calculations of funcA? It appears that the call to funcB itself is very expensive.

Reply via email to