Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing Function for speed performance in CPP

I have created a simple function to convert any lowercase letter a-z to uppercase, the problem may not be a problem but every test returns 0. If I add system("pause") I can see a new value indicating the length of pause.

Is there a more accurate way to test the speed, or is this actually correct? I would like to compare it to other functions to see if it converts faster then standard functions.

char* ToUppercase(char* Input)
{
    int Len = Length(Input);
    for (int i = 0; i < Len; i++)
    {
        short keycode = static_cast<short>(Input[i]);
        if (keycode >= 97 && keycode <= 122)
            Input[i] -= 32;
    }
    return Input;
}

The current timer I use to test is (created by someone else)

template<typename TimeT = std::chrono::milliseconds>
struct measure
{
    template<typename F, typename ...Args>
    static typename TimeT::rep execution(F func, Args&&... args)
    {
        auto start = std::chrono::system_clock::now();
        func(std::forward<Args>(args)...);
        auto duration = std::chrono::duration_cast< TimeT>
            (std::chrono::system_clock::now() - start);
        return duration.count();
    }
};

To call I use:

void Debug()
{
    char Buffer[10000] = "aaaa /..../ aaaa";
    MyStringControl::ToUppercase(Buffer);
}
int main()
{
    std::cout << measure<std::chrono::nanoseconds>::execution(Debug);
}
like image 992
TheStart101 Avatar asked Dec 25 '22 20:12

TheStart101


1 Answers

Did you look at std::chrono::high_resolution_clock?

Here's an example:

#include <iostream>
#include <ctime>
#include <ratio>
#include <chrono>

template<typename TimeT = std::chrono::milliseconds>
struct measure
{
    template<typename F, typename ...Args>
    static typename TimeT::rep execution(F func, Args&&... args)
    {
        auto start = std::chrono::high_resolution_clock::now();
        func(std::forward<Args>(args)...);
        auto duration = std::chrono::duration_cast< TimeT>
                (std::chrono::high_resolution_clock::now() - start);
        return duration.count();
    }
};

int total = 0;

void test()
{
    int foo = 0;
    for (int i=0; i<1000; ++i) ++foo;
    total += foo;
}

int main ()
{
    using namespace std::chrono;

    for (int i = 0; i < 30; ++i)
    {
        total = 0;
        auto t = measure<std::chrono::nanoseconds>::execution(test);
        std::cout << "Calculated total = " << total << " in " << t << " ns." << std::endl;
    }    
    return 0;
}

Which gives:

Calculated total = 1000 in 64 ns.
Calculated total = 1000 in 21 ns.
Calculated total = 1000 in 22 ns.
Calculated total = 1000 in 21 ns.
Calculated total = 1000 in 14 ns.
Calculated total = 1000 in 15 ns.
Calculated total = 1000 in 13 ns.
Calculated total = 1000 in 14 ns.
Calculated total = 1000 in 13 ns.
Calculated total = 1000 in 14 ns.
Calculated total = 1000 in 13 ns.
Calculated total = 1000 in 21 ns.
Calculated total = 1000 in 14 ns.
Calculated total = 1000 in 15 ns.
Calculated total = 1000 in 14 ns.
Calculated total = 1000 in 15 ns.
Calculated total = 1000 in 22 ns.
Calculated total = 1000 in 21 ns.
Calculated total = 1000 in 20 ns.
Calculated total = 1000 in 14 ns.
Calculated total = 1000 in 14 ns.
Calculated total = 1000 in 14 ns.
Calculated total = 1000 in 20 ns.
Calculated total = 1000 in 20 ns.
Calculated total = 1000 in 21 ns.
Calculated total = 1000 in 20 ns.
Calculated total = 1000 in 15 ns.
Calculated total = 1000 in 15 ns.
Calculated total = 1000 in 15 ns.
Calculated total = 1000 in 14 ns.
like image 152
sfjac Avatar answered Dec 28 '22 07:12

sfjac