Boost logo

Boost Users :

From: SD (stefan.demharter_at_[hidden])
Date: 2008-07-09 12:42:15


Hi,

i've encountered a significant performance difference with boost_regex
between linking first to it and linking last to it. I guess it is not a
issue specific to boost_regex, but maybe someone can clarify that.

I have created the following test files and tested with gcc-4.1 and
gcc-4.3 on 64-bit linux and gcc-4.1.2 on 32-bit linux. The results for
64bit are further down, but the results for 32-bit are very similar.

****

# set your boost_extension (i.e. -mt)
# export BOOST_EXT=-mt

cat > test.hpp <<EOF
// test.hpp
#ifndef TEST
#define TEST

#include <string>

int check(const std::string & test);

#endif // TEST
EOF

cat > test.cpp <<EOF
// test.cpp
#include "test.hpp"

#include <boost/regex.hpp>

int check(const std::string & test)
{
    int result = 0;
    static boost::regex
match("a*b*c*d*e*f*g*h*i*j*k*a*b*c*d*e*f*g*h*i*a*b*c*d*e*f*g*h*i*j*");
    for (int i = 0; i < 1000; ++i) {
        result += boost::regex_match(test, match);
    }
    return result;
}
EOF

cat > main.cpp <<EOF
// main.cpp
#include "test.hpp"

#include <iostream>

int main()
{
    std::cout << check("abcdaaaaaaaaaabcdefgaaaaaaaaaaabbbbbbbbbddeef")
<< std::endl;
}
EOF

# Create libtest.so
g++ -shared -fPIC test.cpp -o libtest.so
# link boost_regex before libtest.so
g++ main.cpp -lboost_regex${BOOST_EXT} -ltest -L${PWD} -o main_fast
# link boost_regex after libtest.so
g++ main.cpp -ltest -lboost_regex${BOOST_EXT} -L${PWD} -o main_slow

time env LD_LIBRARY_PATH=${PWD} ./main_slow
# real 0m0.753s, user 0m0.746s, sys 0m0.004s
time env LD_LIBRARY_PATH=${PWD} ./main_fast
# real 0m0.104s, user 0m0.103s, sys 0m0.002s

As the results show the order which libs are being linked to the program
has significant performance impacts on execution time: main_fast is
about 7 times faster than main_slow.

****

Interestingly main_slow becomes much faster, if you compile libtest.so
with optimization. With -O the execution time of main_slow becomes:

g++ -O -shared -fPIC test.cpp -o libtest.so
time env LD_LIBRARY_PATH=${PWD} ./main_slow
# real 0m0.118s, user 0m0.115s, sys 0m0.003s

But it is still about 15% slower than main_fast.

****

If you compile test.cpp statically into the executable you also get slow
results

g++ main.cpp test.cpp -o main_test -l boost_regex
time ./main_test
# real 0m0.645s, user 0m0.641s, sys 0m0.001s

****

If you compile an optimized version of main_test on my 64-bit linux you
also get about 10% worse runtime than the unoptimized version of
main_fast. With 32-bit however main_test runs faster than main_fast.

g++ -O2 main.cpp test.cpp -o main_test -l boost_regex
time ./main_test
# real 0m0.113s, user 0m0.111s, sys 0m0.001s

****

I would appreciate if someone could explain these surprising results.

Regards
Stefan




Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net