[ create a new paste ] login | about

Link: http://codepad.org/17zgzd3K    [ raw code | output | fork ]

C++, pasted on Jul 3:
#include <iostream>
#include <time.h>

double time_sqrt(int n)
{
    clock_t start = clock();

    double v = 2.0;
    double s = 0.0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            s += sqrt(v);
            v += 1.0e-10;
        }
    }
    clock_t end = clock();
    double diff = double(end - start) / CLOCKS_PER_SEC;

    std::cout << "Check: " << s << " Time: " << diff << std::endl;
    return diff;
}

double time_normal(int n)
{
    clock_t start = clock();

    double v = 2.0;
    double s = 0.0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            s += v;
            v += 1.0e-10;
        }
    }
    clock_t end = clock();
    double diff = double(end - start) / CLOCKS_PER_SEC;

    std::cout << "Check: " << s << " Time: " << diff << std::endl;
    return diff;
}



int main()
{
    double t1 = time_sqrt(5000);
    double t2 = time_normal(5000);
    std::cout << "Factor: " << (t1 / t2) << std::endl;

    return 0;
}


Output:
1
2
3
Check: 3.53664e+07 Time: 0.15
Check: 5.00312e+07 Time: 0.05
Factor: 3


Create a new paste based on this one


Comments: