473,785 Members | 2,489 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is incrementing iterators actually this slow?

Hey all,

I have a bit of code that implements a suffix array, which is essentially an
array of iterators or indexes into a string. The array starts out like this

String: ABAB
Array: 0123

But then gets sorted

Array: 2031
2 AB
0 ABAB
3 B
1 BAB

All of this is well and good, but I have a little problem. Currently, I am
using the STL's sort algorithm and providing a custom comparison function,
which ends up sorting this vector<string:: iterator> alphabetically. Here is
the comparison and other associated functions:

bool itLess(string:: const_iterator it1, string::const_i terator it2, int
skip)
{
advanceIterator s(it1, it2, skip);
return (*it1 < *it2 || *it1 == '\0');
}

int advanceIterator s(string::const _iterator &it1, string::const_i terator
&it2,
int skip)
{
string::const_i terator ref(it1);
advanceNoCheck( it1, it2, skip);

while (*it1 == *it2 && !(*it1 == '\0')) {
++it1;
++it2;
}

return it1 - ref;
}

void advanceNoCheck( string::const_i terator &it1, string::const_i terator
&it2,
int skip)
{
if (skip) {
it1 += skip;
it2 += skip;
}
}

The way the sorting works is that one method goes through the text string
and makes an iterator for each position, putting it into the array.
Afterwards, I call 'sort(m_array.b egin(), m_array.end(), &itLess).' It
takes entirely too long.

I have profiled my code with gprof (I know, it's platform specific, but you
should be able to read the output). The code was compiled with all
optimizations turned on, and run on a text of 10 million characters that did
not have a high incidence of repetition. Here is the output of the
profiler:

% cumulative self self total
time seconds seconds calls Ks/call Ks/call name
int advanceIterator s(string::const _iterator &, string::const_i terator &):
98.92 1196.55 1196.55 302747982 0.00 0.00
bool itLess(string:: const_iterator, string::const_i terator):
0.42 1201.59 5.04 282747983 0.00 0.00
std::__unguarde d_partition([template junk]):
0.21 1204.10 2.51 1074094 0.00 0.00
void advanceNoCheck( string::const_i terator &, string::const_i terator &):
0.09 1205.20 1.10 302747982 0.00 0.00

Clearly, my advanceIterator s (and hence, all of my iterator comparison) is
far too slow. If I run my code without optimizations (that is, if I do not
inline iterator::opera tor ++ and iterator::opera tor *), you would see that
most of the time spent in advanceIterator s is actually spent in those two
methods. Does anyone know a faster way to sort string::const_i terators
lexicographical ly?

- JFA1
Jul 23 '05 #1
3 2448
"James Aguilar" <jf**@cec.wustl .edu> wrote in message
news:d2******** **@newsreader.w ustl.edu...
bool itLess(string:: const_iterator it1, string::const_i terator it2, int
skip)
{
advanceIterator s(it1, it2, skip);
return (*it1 < *it2 || *it1 == '\0');
}
I suspect that this code doesn't do what you think it does.

I am guessing that you are using the comparison *it1 == '\0' as a way of
checking whether it1 has run off the end of the string.

It doesn't do that. It is your responsibility to know where the end of the
string is, and to dereference only iterators that you know refer to elements
of the string.

int advanceIterator s(string::const _iterator &it1, string::const_i terator
&it2,
int skip)
{
string::const_i terator ref(it1);
advanceNoCheck( it1, it2, skip);

while (*it1 == *it2 && !(*it1 == '\0')) {
++it1;
++it2;
}

return it1 - ref;
}


Same problem here: It looks like you are assuming that once you run off the
end of the string, *it1 will be 0. It won't, in general. You'll get what
you get.
Jul 23 '05 #2
"Andrew Koenig" <ar*@acm.org> wrote in message
news:ii******** ***********@bgt nsc04-news.ops.worldn et.att.net...

Same problem here: It looks like you are assuming that once you run off
the end of the string, *it1 will be 0. It won't, in general. You'll get
what you get.


It seems to work on small problems (sorting them correctly. Also, a little
test code shows that, at least with g++ 3.3.3, it works:

#include <string>
#include <iostream>

using namespace std;

int main()
{
string a;

cout << (*(a.begin() + a.length()) == '\0') << '\n';

a += "asad";
cout << (*(a.begin() + a.length()) == '\0') << '\n';

a = "OMGZ?";
cout << (*(a.begin() + a.length()) == '\0') << '\n';

return 0;
}

prints 1, 1, 1 on my system. Even if this is non-standard, I'm almost
positive that it is not the problem.

- JFA1
Jul 23 '05 #3
"James Aguilar" <jf**@cec.wustl .edu> wrote in message
news:d2******** **@newsreader.w ustl.edu...
It seems to work on small problems (sorting them correctly. Also, a
little test code shows that, at least with g++ 3.3.3, it works:
No, it happens to do something that appears to work in this particular case.
prints 1, 1, 1 on my system. Even if this is non-standard, I'm almost
positive that it is not the problem.


It doesn't matter. As written, the program's behavior is undefined.
There's no point in investigating further until this problem is corrected.
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2553
by: Jess Austin | last post by:
hi, I like the way that Python does lists, and I love the way it does iterators. But I've decided I don't like what it does with iterators of lists. Lists are supposed to be mutable sequences, but try to use an iterator of a list that you're mutating and watch it all fall to pieces. That is, if you change the length of a section of the list through which the iterator has already passed, it will lose track of where it is. I think...
2
4395
by: Sanyi | last post by:
Hello, I've did some some numerical programs in C/C++ before, but decided to use STL to write neater code. Then again I don't want to give up speed so I have a couple of questions. Apparently the recommended structure for a fast numeric code is valarray. Alas, from the sparse documentation in the books I looked into and a google search it appears that there are no iterators associated to them. I tried to "simulate" them with pointers but...
3
3886
by: Robin Tucker | last post by:
Hi, I'm in the process of implementing a multi-user system containing an adjacency list (tree structure). I'm using a TIMESTAMP field on each record in the adjacency list in order to tell when a node has been changed since the last read. Sometimes though, it is useful to flag a "parent" (or all ancestors or a node) as being changed if any of its children have. Is there any way I can force an update to the parent TIMESTAMP field...
8
1994
by: babak | last post by:
Hi everyone I have a problem with Iterators and containers in STL that hopefully someone can help me with. This is what I try to do: I have an associative (map) container and I have a function where I with the help of iterators want to search through the container and remove a certain object in the container. Here is part of the code in that function:
14
2326
by: Jiri Kripac | last post by:
Languages such as Simula 67 contain a general concept of coroutines that allow the execution of a method to be suspended without rolling back the stack and then later resumed at the same place as it has been suspended. The C# iterators seem to be a special case of this general suspend/resume concept. The "yield" statement suspends the execution of the current method and calling MoveNext() resumes it. I think it would be cleaner to...
19
7554
by: John | last post by:
In STL's map implementation, the distance between two iterators it1, it2 takes O(K) to compute where K is the actual distance between the two iterators. In theory, a red black tree can do this in O(log K) time. Anyone knows if there is a way to do this in map<> or if map<> can be inherited/modified to do this? Thanks, --j
6
10971
by: Shiraz | last post by:
Me and my colleagues have been working on a problem since yesterday and can’t seem to find a solution to it. It relates in a good measure to iterators and the way we’re using them in our project. I will first give you a little introduction to our main task. We had at our hands entire modules which were written in C++. These were to communicate with a stack written in C, and in turn act as a layer between the C code and C# code used in...
2
1176
by: Abubakar | last post by:
Hi, lets say I have a iterator of the type: list<string>::const_iterator itr; lets say its at some position in a given list. Now I can goto the next item by simply doing a itr++. But I want to increment more than just 1. Lets says I want to increment the itr 3 times, ie being able to do something like itr+=3. But as the += is not defined for that iterator class, I cant do that. What should I do?
18
2121
by: desktop | last post by:
1) I have this code: std::list<intmylist; mylist.push_back(1); mylist.push_back(2); mylist.push_back(3); mylist.push_back(4);
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10161
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8986
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7506
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5523
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3662
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2890
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.