Static regex object or does it matter?

void.pointer

Suppose I have the following function:

bool IsNumber(std::string const& str)
{
    return std::regex_match(str, std::regex{"\\d+"});
}

I am constructing the std::regex each call. Is there documented performance overhead by doing this? Would it be better to make it static instead, like below?

bool IsNumber(std::string const& str)
{
    static std::regex const number_regex{"\\d+"};
    return std::regex_match(str, number_regex);
}

Or does it not really matter?

Zacharias

The compiler might not be able to identify if the construction of std::regex is equal everytime it gets called (e.g. constructor could access a static/global variable). Thus the safe way would be to construct it in any case. On the other hand, compilers nowadays are very intelligent, maybe he parses the constructor deep enough to realize it must be constant over time, thus optimizes the thing out. In any case: profile it. e.g. make a loop and measure the time (std::chrono) for a few thousand calls (at least in order of seconds). –

I've made a very simple test programm to profile it:

#include <stdio.h>
#include <regex>
#include <chrono>

bool IsNumberA( std::string const& str )
{
    return std::regex_match( str, std::regex { "\\d+" } );
}

static std::regex number_regex( "\\d+" );
bool IsNumberB( std::string const& str )
{
    return std::regex_match( str, number_regex );
}

void main()
{
    size_t count = 100000;

    std::vector<std::string> aRandomStrings;

    for( size_t i = 0; i < count; i++ )
        aRandomStrings.push_back((rand() % 2 == 0) ? "nonumberatall" : "3141592");

    auto time = std::chrono::system_clock::now();

    size_t numberCountA = 0;
    for( size_t i = 0; i < count; i++ )
        if( IsNumberA( aRandomStrings[i] ) )
            numberCountA++;

    auto takenTimeA = std::chrono::duration_cast<std::chrono::milliseconds>
        (std::chrono::system_clock::now() - time);
    time = std::chrono::system_clock::now();    // reset

    size_t numberCountB = 0;
    for( size_t i = 0; i < count; i++ )
        if( IsNumberB( aRandomStrings[i] ) )
            numberCountB++;

    auto takenTimeB = std::chrono::duration_cast<std::chrono::milliseconds>
        (std::chrono::system_clock::now() - time);

    printf( "took %d ms for A, %d ms for B\n", takenTimeA.count(), takenTimeB.count() );
}

Results

I've compiled it without optimizations too, just to see if the compiler (msvc) is smart enough.

A 6283ms, B 41ms

Optimized: A 268ms, B 85ms

We can clearly see a massive boost in performance when using a predefined variable (B). The slower release in case B is not really clear to me, but the time scale is and might be too low. Also there might be a lot of unknown stuff in the random generator too.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java : Why does placement of static variable matter?

From Dev

Why does the order of alternatives matter in regex?

From Dev

Why does the declared type of an object matter at runtime?

From Dev

Why Does it Matter if An Object is Nil When Destroyed?

From Dev

Why Does it Matter if An Object is Nil When Destroyed?

From Dev

Does it matter (performance-wise) where a static function is defined?

From Dev

Does it matter (performance-wise) where a static function is defined?

From Dev

Flask + Nginx proxy: Does it matter which one serves static files?

From Dev

Does it matter which static IP address I choose for my devices?

From Dev

Does the type of image matter when training an object detector?

From Dev

Why does order of `Object.include` and `Fixnum.prepend` matter?

From Dev

static properites does not exist into the object

From Dev

Does it matter which operand of integer division is static_casted to obtain a float result?

From Dev

Does it matter which operand of integer division is static_casted to obtain a float result?

From Java

Does annotations order matter?

From Dev

Does order of conditions in $and matter?

From Dev

value for serialVersionUID does it matter?

From Dev

Why does distro matter?

From Dev

Why does distro matter?

From Dev

value for serialVersionUID does it matter?

From Dev

Heartbleed but - does authorization matter?

From Dev

Does OS matter?

From Dev

Does the syntax of not equal matter?

From Dev

Does deserialized object preserve static values?

From Dev

Does order in sed's regex character classes (e.g. [abc]) matter?

From Dev

Does it matter if function returns T or object if T can not be inferred from context?

From Dev

Why does it matter what object I use wait()/notify() on, if I just want a way to signal between threads?

From Dev

When defining vertexes for shapes in object-space using Opengl does it matter what the range of x,y and z are

From Dev

When does operator associativity matter?

Related Related

  1. 1

    Java : Why does placement of static variable matter?

  2. 2

    Why does the order of alternatives matter in regex?

  3. 3

    Why does the declared type of an object matter at runtime?

  4. 4

    Why Does it Matter if An Object is Nil When Destroyed?

  5. 5

    Why Does it Matter if An Object is Nil When Destroyed?

  6. 6

    Does it matter (performance-wise) where a static function is defined?

  7. 7

    Does it matter (performance-wise) where a static function is defined?

  8. 8

    Flask + Nginx proxy: Does it matter which one serves static files?

  9. 9

    Does it matter which static IP address I choose for my devices?

  10. 10

    Does the type of image matter when training an object detector?

  11. 11

    Why does order of `Object.include` and `Fixnum.prepend` matter?

  12. 12

    static properites does not exist into the object

  13. 13

    Does it matter which operand of integer division is static_casted to obtain a float result?

  14. 14

    Does it matter which operand of integer division is static_casted to obtain a float result?

  15. 15

    Does annotations order matter?

  16. 16

    Does order of conditions in $and matter?

  17. 17

    value for serialVersionUID does it matter?

  18. 18

    Why does distro matter?

  19. 19

    Why does distro matter?

  20. 20

    value for serialVersionUID does it matter?

  21. 21

    Heartbleed but - does authorization matter?

  22. 22

    Does OS matter?

  23. 23

    Does the syntax of not equal matter?

  24. 24

    Does deserialized object preserve static values?

  25. 25

    Does order in sed's regex character classes (e.g. [abc]) matter?

  26. 26

    Does it matter if function returns T or object if T can not be inferred from context?

  27. 27

    Why does it matter what object I use wait()/notify() on, if I just want a way to signal between threads?

  28. 28

    When defining vertexes for shapes in object-space using Opengl does it matter what the range of x,y and z are

  29. 29

    When does operator associativity matter?

HotTag

Archive