Mailinglist Archive: zypp-devel (226 mails)

< Previous Next >
[zypp-devel] Test of boost::replace_all
  • From: Josef Reidinger <jreidinger@xxxxxxx>
  • Date: Tue, 22 Apr 2008 11:03:43 +0200
  • Message-id: <480DA9EF.5070804@xxxxxxx>
I use in some place boost function replace all. I see in zypper, that this function jano implement. So I test which is faster and also contain gsub from zypp String which is not in place (so must copy result).
Due to result I add janpo's implementation to zypp and replace all boost::replace_all

I attach test source.
Here is result (10000 and 10000000 loops):
zypper gsub boost
user time: 9
system time: 0
jano replace_all boost
user time: 7
system time: 0
boost replace_all boost
user time: 84
system time: 0
jreidinger@zofie:~/tmp/testing>
jreidinger@zofie:~/tmp/testing> g++ test.cc
jreidinger@zofie:~/tmp/testing> ./a.out
zypper gsub boost
user time: 769
system time: 0
jano replace_all boost
user time: 689
system time: 0
boost replace_all boost
user time: 8626
system time: 6

Pepa
#include <string>
#include <iostream>
#include <fstream>
#include <sys/param.h>
#include <sys/times.h>
#include <sys/types.h>
#include <boost/algorithm/string/replace.hpp>

#define LOOP 10000000

struct tms t,u;

using namespace std;

string gsub(const string& sData, const string& sFrom, const string& sTo)
{
string sNew = sData;
if (! sNew.empty())
{
string::size_type toLen = sTo.length();
string::size_type frLen = sFrom.length();
string::size_type loc = 0;
while (string::npos != (loc = sNew.find(sFrom, loc)))
{
sNew.replace(loc, frLen, sTo);
loc += toLen;
if (loc >= sNew.length())
break;
}
}
return sNew;
}

string & jano_replace_all(string & str, const string & from, const string & to)
{
string::size_type pos = 0;
while((pos = str.find(from, pos)) != string::npos)
{
str.replace(pos, from.size(), to);
pos += to.size();
}
return str;
}

int main(){
std::ofstream nul("/dev/null",ios_base::out);

cout << "zypper gsub boost" <<endl;
times(&t);
for (int i=0;i<LOOP;i++)
{
string test("test string hello world \n ::: do :::");
nul << gsub(test,":",";");
}
times(&u);
cout << "user time: " << u.tms_utime-t.tms_utime << endl;
cout << "system time: " << u.tms_stime-t.tms_stime << endl;

cout << "jano replace_all boost" <<endl;
times(&t);
for (int i=0;i<LOOP;i++)
{
string test("test string hello world \n ::: do :::");
nul << jano_replace_all(test,":",";");
}
times(&u);
cout << "user time: " << u.tms_utime-t.tms_utime << endl;
cout << "system time: " << u.tms_stime-t.tms_stime << endl;

cout << "boost replace_all boost" <<endl;
times(&t);
for (int i=0;i<LOOP;i++)
{
string test("test string hello world \n ::: do :::");
boost::replace_all(test,":",";");
nul << test;
}
times(&u);
cout << "user time: " << u.tms_utime-t.tms_utime << endl;
cout << "system time: " << u.tms_stime-t.tms_stime << endl;

return 0;
}
< Previous Next >