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: Fri, 25 Apr 2008 08:04:56 +0200
- Message-id: <48117488.6090002@xxxxxxx>
Michael Matz wrote:
Yes, you are right and also one more bug in code (I add toLen, but need frLen). So now after fix results is not so exciting. Conclusion is, that new gsub is better then old and replace_all is better then new_gsub except for longer 'to' string. (but if you need new string and not replacing in-place then new gsub is better).
Fixed code of tests and new gsub is attached.
same size of replacing strings:
new gsub
user time: 570
system time: 0
jano replace_all
user time: 554
system time: 1
old gsub replace_all
user time: 629
system time: 1
to is bigger:
new gsub
user time: 794
system time: 2
jano replace_all
user time: 861
system time: 2
old gsub replace_all
user time: 838
system time: 1
from is bigger:
new gsub
user time: 404
system time: 0
jano replace_all
user time: 353
system time: 2
old gsub replace_all
user time: 417
system time: 2
#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-oldLoc);
sNew.append(sTo);
loc += frLen;
oldLoc = loc;
if (loc >= sData.length())
break;
}
if (oldLoc!=sData.size())
sNew.append(sData,oldLoc,sData.size()-oldLoc);
}
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;
}
string old_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;
}
int main(){
std::ofstream nul("/dev/null",ios_base::out);
//std::ostream& nul = cout;
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;
cout << "old gsub replace_all" <<endl;
times(&t);
for (int i=0;i<LOOP;i++)
{
string test("test string hello world \n ::: do :::");
nul << old_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;
return 0;
}
Hi,
On Thu, 24 Apr 2008, Josef Reidinger wrote:
So I implement it (code of it in test file which I attach). Speed improve is really significant.
Well, nice, but it doesn't do what it's supposed to. You probably want to check some results manually. In particular:
while (string::npos != (loc = sData.find(sFrom, loc)))
{
sNew.append(sData,oldLoc,loc);
The third argument to append is a length, not an offset.
sNew.append(sTo);
loc += toLen;
oldLoc = loc;
if (loc >= sNew.length())
break;
Get rid of this if and break. It's meanwhile wrong I think.
Ciao,
Michael.
Yes, you are right and also one more bug in code (I add toLen, but need frLen). So now after fix results is not so exciting. Conclusion is, that new gsub is better then old and replace_all is better then new_gsub except for longer 'to' string. (but if you need new string and not replacing in-place then new gsub is better).
Fixed code of tests and new gsub is attached.
same size of replacing strings:
new gsub
user time: 570
system time: 0
jano replace_all
user time: 554
system time: 1
old gsub replace_all
user time: 629
system time: 1
to is bigger:
new gsub
user time: 794
system time: 2
jano replace_all
user time: 861
system time: 2
old gsub replace_all
user time: 838
system time: 1
from is bigger:
new gsub
user time: 404
system time: 0
jano replace_all
user time: 353
system time: 2
old gsub replace_all
user time: 417
system time: 2
#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-oldLoc);
sNew.append(sTo);
loc += frLen;
oldLoc = loc;
if (loc >= sData.length())
break;
}
if (oldLoc!=sData.size())
sNew.append(sData,oldLoc,sData.size()-oldLoc);
}
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;
}
string old_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;
}
int main(){
std::ofstream nul("/dev/null",ios_base::out);
//std::ostream& nul = cout;
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;
cout << "old gsub replace_all" <<endl;
times(&t);
for (int i=0;i<LOOP;i++)
{
string test("test string hello world \n ::: do :::");
nul << old_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;
return 0;
}
| < Previous | Next > |