Mailinglist Archive: zypp-devel (226 mails)

< Previous Next >
Re: [zypp-devel] Re: [zypp-commit] r9774 - /trunk/zypper/src/zypper-utils.cc
  • From: Josef Reidinger <jreidinger@xxxxxxx>
  • Date: Thu, 24 Apr 2008 16:36:19 +0200
  • Message-id: <48109AE3.7000405@xxxxxxx>
Michael Matz wrote:
Hi,

On Wed, 23 Apr 2008, josef reidiner wrote:

Interesting, I think gsub works little different so I try implement little
different version. Problem with replace on string with different length of
substitute is that each call must move some char in internall char
strorage...so if gsub instead of replacing append can be better

This is true, but not how gsub currently is implemented. If you do that, then yes, gsub might potentially be faster.

I test it on Thursday or Friday and write here result.

Cool.


Ciao,
Michael.

So I implement it (code of it in test file which I attach). Speed improve is really significant.

here is my test results:

(different size change : for ;; )
new gsub
user time: 377
system time: 2
jano replace_all
user time: 810
system time: 1


(same size change : for ;) (this quite surprise me)
new gsub
user time: 364
system time: 1
jano replace_all
user time: 530
system time: 3

(remove - change : for ;)
new gsub
user time: 368
system time: 0
jano replace_all
user time: 622
system time: 2


So what you recommend??? remove replace_all and have only new gsub or stay both?

welcome any comments on code
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; sNew.reserve(sData.size());
if (! sData.empty())
{
string::size_type toLen = sTo.length();
string::size_type frLen = sFrom.length();
string::size_type loc = 0;
string::size_type oldLoc = 0;
while (string::npos != (loc = sData.find(sFrom, loc)))
{
sNew.append(sData,oldLoc,loc);
sNew.append(sTo);
loc += toLen;
oldLoc = loc;
if (loc >= sNew.length())
break;
}
sNew.append(sData,oldLoc,loc);
}
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);
string from(":");
string to(";;");

cout << "new gsub" <<endl;
times(&t);
for (int i=0;i<LOOP;i++)
{
string test("test string hello world \n ::: do :::");
nul << gsub(test,from,to);
}
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" <<endl;
times(&t);
for (int i=0;i<LOOP;i++)
{
string test("test string hello world \n ::: do :::");
nul << jano_replace_all(test,from,to);
}
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 >
Follow Ups