Weird performance results

时间:2015-11-12 12:05:41

标签: c++ performance

I have a generated lexer. The lexer takes a callback in a parameter and calls it each time a match is found. Callback's prototype is:

void(const char *matchBegin, const char *matchEnd, unsigned matchId);

I am testing it with two callbacks:

Empty callback:

[] (const char *, const char *, unsigned)
{}

Incrementing counter callback:

[&counter] (const char *, const char *, unsigned id)
{
    counter += id;
}

Here are the measurements for 100MB of input (there is no difference between programs except those callbacks):

gcc 5.2 empty calback: Avg: 582.3ms, StdDev: 11.3ms (run 20 times)

gcc 5.2 increment callback: Avg: 396.6ms, StdDev: 1.68ms (run 20 times)

clang 3.7 empty calback: 402ms

clang 3.7 increment callback: 577ms

My intuition is that additional increment operation should cost us time. Apparently it does not in case of gcc. Can anyone explain why?

EDIT: @Joachim: The lexer accepts any function object in a parameter:

template <typename M, typename Action>
inline void tokenize(const M &stateMachine, const char *&begin,
    const char *end, Action action);

The time is measured automatically by gtest. Exactly one test is run. The state machine generation is not taken into account.

0 个答案:

没有答案