473,486 Members | 2,394 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

STL: Could you make this snippet more efficient

Dear comp.lang.c++,

Could you make this snippet more efficient? As you see I have too many
variables introduced in the code.

//Read set of integers from a file on line by line basis in a STL set
//fp is pre-defined
for(;!fp.eof();)
{
string linestr;
getline(fp,linestr);
istringstream strstreamline(linestr);
istream_iterator<unsigned intintstream(strstreamline);
set<unsigned intpckset;
copy ( intstream , istream_iterator<unsigned int>() ,
inserter(modpckset,modpckset.begin ( ) ));
}

Thanks in advance.
Dec 3 '07 #1
12 1706
pe******@gmail.com wrote:
Dear comp.lang.c++,

Could you make this snippet more efficient? As you see I have too many
variables introduced in the code.

//Read set of integers from a file on line by line basis in a STL set
//fp is pre-defined
for(;!fp.eof();)
{
string linestr;
getline(fp,linestr);
istringstream strstreamline(linestr);
istream_iterator<unsigned intintstream(strstreamline);
set<unsigned intpckset;
What's that for?
copy ( intstream , istream_iterator<unsigned int>() ,
inserter(modpckset,modpckset.begin ( ) ));
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 3 '07 #2
On Dec 3, 11:37 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
pedag...@gmail.com wrote:
Dear comp.lang.c++,
Could you make this snippet more efficient? As you see I have too many
variables introduced in the code.
//Read set of integers from a file on line by line basis in a STL set
//fp is pre-defined
for(;!fp.eof();)
{
string linestr;
getline(fp,linestr);
istringstream strstreamline(linestr);
istream_iterator<unsigned intintstream(strstreamline);
set<unsigned intpckset;

What's that for?
copy ( intstream , istream_iterator<unsigned int>() ,
inserter(modpckset,modpckset.begin ( ) ));
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
*Correction* 'pckset' should have been 'modpckset'.
Dec 3 '07 #3
pe******@gmail.com wrote:
On Dec 3, 11:37 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>pedag...@gmail.com wrote:
>>Dear comp.lang.c++,
>>Could you make this snippet more efficient? As you see I have too
many variables introduced in the code.
>>//Read set of integers from a file on line by line basis in a STL
set //fp is pre-defined
for(;!fp.eof();)
{
string linestr;
getline(fp,linestr);
istringstream strstreamline(linestr);
istream_iterator<unsigned intintstream(strstreamline);
set<unsigned intpckset;

What's that for?
>>copy ( intstream , istream_iterator<unsigned int>() ,
inserter(modpckset,modpckset.begin ( ) ));
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

*Correction* 'pckset' should have been 'modpckset'.
OK. The entire snippet can be made more efficient by removing
the lines between

getline(fp, linestr);

and

}

; they do essentially no work that would have side effects.
The creation of a stringstream, reading stuff from it, and
putting those unsigned ints into a local set are a waste of
CPU cycles.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 3 '07 #4
pe******@gmail.com wrote:
Dear comp.lang.c++,

Could you make this snippet more efficient? As you see I have too many
variables introduced in the code.

//Read set of integers from a file on line by line basis in a STL set
//fp is pre-defined
for(;!fp.eof();)
Hint. The for statement does not do what you think it does.
Also, why are you using a for() instead of a while() in this situation?
{
string linestr;
getline(fp,linestr);
istringstream strstreamline(linestr);
istream_iterator<unsigned intintstream(strstreamline);
set<unsigned intpckset;
copy ( intstream , istream_iterator<unsigned int>() ,
inserter(modpckset,modpckset.begin ( ) ));
}

Thanks in advance.
Dec 3 '07 #5
On Dec 3, 11:50 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
pedag...@gmail.com wrote:
On Dec 3, 11:37 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
pedag...@gmail.com wrote:
Dear comp.lang.c++,
>Could you make this snippet more efficient? As you see I have too
many variables introduced in the code.
>//Read set of integers from a file on line by line basis in a STL
set //fp is pre-defined
for(;!fp.eof();)
{
string linestr;
getline(fp,linestr);
istringstream strstreamline(linestr);
istream_iterator<unsigned intintstream(strstreamline);
set<unsigned intpckset;
What's that for?
>copy ( intstream , istream_iterator<unsigned int>() ,
inserter(modpckset,modpckset.begin ( ) ));
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
*Correction* 'pckset' should have been 'modpckset'.

OK. The entire snippet can be made more efficient by removing
the lines between

getline(fp, linestr);

and

}

; they do essentially no work that would have side effects.
The creation of a stringstream, reading stuff from it, and
putting those unsigned ints into a local set are a waste of
CPU cycles.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Insightful perspective. It just me that I dont have to necessarily
deal them as integers and could consider them as strings instead.
Appreciate your help.

However, I need to store the parsed result in a container on which I
could apply set_difference algorithm. Looks like I must resort to
stringstream in this case. Please correct me if I am wrong.

Best.
Dec 3 '07 #6
On Dec 3, 11:51 am, red floyd <no.s...@here.dudewrote:
pedag...@gmail.com wrote:
Dear comp.lang.c++,
Could you make this snippet more efficient? As you see I have too many
variables introduced in the code.
//Read set of integers from a file on line by line basis in a STL set
//fp is pre-defined
for(;!fp.eof();)

Hint. The for statement does not do what you think it does.
Also, why are you using a for() instead of a while() in this situation?
{
string linestr;
getline(fp,linestr);
istringstream strstreamline(linestr);
istream_iterator<unsigned intintstream(strstreamline);
set<unsigned intpckset;
copy ( intstream , istream_iterator<unsigned int>() ,
inserter(modpckset,modpckset.begin ( ) ));
}
Thanks in advance.
Thank you, got it.
Dec 3 '07 #7
pe******@gmail.com wrote:
On Dec 3, 11:50 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>pedag...@gmail.com wrote:
>>On Dec 3, 11:37 am, "Victor Bazarov" <v.Abaza...@comAcast.net>
wrote:
pedag...@gmail.com wrote:
Dear comp.lang.c++,
>>>>Could you make this snippet more efficient? As you see I have too
many variables introduced in the code.
>>>>//Read set of integers from a file on line by line basis in a STL
set //fp is pre-defined
for(;!fp.eof();)
{
string linestr;
getline(fp,linestr);
istringstream strstreamline(linestr);
istream_iterator<unsigned intintstream(strstreamline);
set<unsigned intpckset;
>>>What's that for?
>>>>copy ( intstream , istream_iterator<unsigned int>() ,
inserter(modpckset,modpckset.begin ( ) ));
}
>>>V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
>>*Correction* 'pckset' should have been 'modpckset'.

OK. The entire snippet can be made more efficient by removing
the lines between

getline(fp, linestr);

and

}

; they do essentially no work that would have side effects.
The creation of a stringstream, reading stuff from it, and
putting those unsigned ints into a local set are a waste of
CPU cycles.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Insightful perspective. It just me that I dont have to necessarily
deal them as integers and could consider them as strings instead.
Appreciate your help.

However, I need to store the parsed result in a container on which I
could apply set_difference algorithm. Looks like I must resort to
stringstream in this case. Please correct me if I am wrong.
If you have to convert them, you should use some kind of conversion
function, be it strtod, sscanf, or istream::operator>>. Actually,
it is quite possible that operator>internally uses strtod.

If storing them in a set is a requirements, you probably need to
do your 'copy' with the second argument being 'inserter' for some
other, non-local set.

It is possible that once the string is read, you might have better
luck using 'strtod' yourself (essentially replacing what operator>>
does, skipping WS, etc.) However, without measuring it's hard to
say what area of your algorithm you need to address.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 3 '07 #8
pe******@gmail.com wrote:
Dear comp.lang.c++,

Could you make this snippet more efficient? As you see I have too many
variables introduced in the code.

//Read set of integers from a file on line by line basis in a STL set
//fp is pre-defined
for(;!fp.eof();)
{
string linestr;
getline(fp,linestr);
istringstream strstreamline(linestr);
istream_iterator<unsigned intintstream(strstreamline);
set<unsigned intpckset;
copy ( intstream , istream_iterator<unsigned int>() ,
inserter(modpckset,modpckset.begin ( ) ));
}
Efficiency doesn't have much to do with number of variables, however, it
probably *is* more efficient to stream your results into a vector, sort
it, and then apply set_difference (which, of course, doesn't actually
require a std::set). One sort at the end is likely to be faster than
maintaining a sorted structure throughout the construction.

Mark
Dec 3 '07 #9
pe******@gmail.com wrote:
Could you make this snippet more efficient? As you see I have too many
variables introduced in the code.

//Read set of integers from a file on line by line basis in a STL set
//fp is pre-defined
for(;!fp.eof();)
{
string linestr;
getline(fp,linestr);
istringstream strstreamline(linestr);
istream_iterator<unsigned intintstream(strstreamline);
set<unsigned intpckset;
copy ( intstream , istream_iterator<unsigned int>() ,
inserter(modpckset,modpckset.begin ( ) ));
}

Thanks in advance.
First let's clean up the code you have.

set< unsigned int modpckset; // has to be defined outside the loop
string line;
while ( getline( fp, line ) ) // proper way to read until end of file
{
istringstream ss( line );
copy( istream_iterator< unsigned int >( ss ),
istream_iterator< unsigned int >(),
inserter( modpckset, modpckset.begin() ) );
}

With the above code, getline will read in a line of the file at a time,
then parse it out to a bunch of unsigned ints. We don't need to do the
extra step:

set< unsigned int modpckset;
copy( istream_iterator< unsigned int >( fp ),
istream_iterator< unsigned int >(),
inserter( modpckset, modpckset.begin() ) );

The above will do the same thing.

Now, if your goal was to read each line into a separate set, say you
want to end up with a vector of sets, then you would need to read each
line separately.

vector< set< unsigned int all_sets;
string line;
while ( getline( fp, line ) )
{
istringstream ss( line );
all_sets.push_back( set< unsigned int >() );
copy( istream_iterator< unsigned int >( ss ),
istream_iterator< unsigned int >(),
inserter( all_sets.back(), all_sets.back().begin() ) );
}
Dec 3 '07 #10
On Dec 3, 3:23 pm, "Daniel T." <danie...@earthlink.netwrote:
pedag...@gmail.com wrote:
Could you make this snippet more efficient? As you see I have too many
variables introduced in the code.
//Read set of integers from a file on line by line basis in a STL set
//fp is pre-defined
for(;!fp.eof();)
{
string linestr;
getline(fp,linestr);
istringstream strstreamline(linestr);
istream_iterator<unsigned intintstream(strstreamline);
set<unsigned intpckset;
copy ( intstream , istream_iterator<unsigned int>() ,
inserter(modpckset,modpckset.begin ( ) ));
}
Thanks in advance.

First let's clean up the code you have.

set< unsigned int modpckset; // has to be defined outside the loop
string line;
while ( getline( fp, line ) ) // proper way to read until end of file
{
istringstream ss( line );
copy( istream_iterator< unsigned int >( ss ),
istream_iterator< unsigned int >(),
inserter( modpckset, modpckset.begin() ) );

}

With the above code, getline will read in a line of the file at a time,
then parse it out to a bunch of unsigned ints. We don't need to do the
extra step:

set< unsigned int modpckset;
copy( istream_iterator< unsigned int >( fp ),
istream_iterator< unsigned int >(),
inserter( modpckset, modpckset.begin() ) );

The above will do the same thing.

Now, if your goal was to read each line into a separate set, say you
want to end up with a vector of sets, then you would need to read each
line separately.

vector< set< unsigned int all_sets;
string line;
while ( getline( fp, line ) )
{
istringstream ss( line );
all_sets.push_back( set< unsigned int >() );
copy( istream_iterator< unsigned int >( ss ),
istream_iterator< unsigned int >(),
inserter( all_sets.back(), all_sets.back().begin() ) );
}
Very nice. If its not too much clutter one could do the same as

set< unsigned int modpckset; // has to be defined outside the loop
string line;
while ( getline( fp, line ) ) // proper way to read until end of file
{
copy( istream_iterator< unsigned int >( istringstream (line) ),
istream_iterator< unsigned int >(),
inserter( modpckset, modpckset.begin() ) );
//...
}
Dec 3 '07 #11
Daniel T. wrote:
[..]
set< unsigned int modpckset; // has to be defined outside the loop
string line;
while ( getline( fp, line ) ) // proper way to read until end of file
{
istringstream ss( line );
copy( istream_iterator< unsigned int >( ss ),
istream_iterator< unsigned int >(),
inserter( modpckset, modpckset.begin() ) );
}

With the above code, getline will read in a line of the file at a
time, then parse it out to a bunch of unsigned ints. We don't need to
do the extra step:

set< unsigned int modpckset;
copy( istream_iterator< unsigned int >( fp ),
istream_iterator< unsigned int >(),
inserter( modpckset, modpckset.begin() ) );

The above will do the same thing.
Really? Will it? What if it can't copy (there is 'O' instead of '0'
somewhere in the file)? Will it attempt to read again from the next
line?
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 3 '07 #12
"Victor Bazarov" <v.********@comAcast.netwrote:
Daniel T. wrote:
[..]
set< unsigned int modpckset; // has to be defined outside the loop
string line;
while ( getline( fp, line ) ) // proper way to read until end of file
{
istringstream ss( line );
copy( istream_iterator< unsigned int >( ss ),
istream_iterator< unsigned int >(),
inserter( modpckset, modpckset.begin() ) );
}

With the above code, getline will read in a line of the file at a
time, then parse it out to a bunch of unsigned ints. We don't need to
do the extra step:

set< unsigned int modpckset;
copy( istream_iterator< unsigned int >( fp ),
istream_iterator< unsigned int >(),
inserter( modpckset, modpckset.begin() ) );

The above will do the same thing.

Really? Will it? What if it can't copy (there is 'O' instead of '0'
somewhere in the file)? Will it attempt to read again from the next
line?
Sorry, at some point while writing the post, I had written that I was
assuming that the file contained all unsigned ints. I must have
accidentally cut that line out.
Dec 4 '07 #13

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

Similar topics

25
2233
by: rokia | last post by:
in a project, I use many,many stl such as stack,list,vctor etc. somewhere the vector's size is more than 2K. is this a efficient way?
9
1863
by: Aguilar, James | last post by:
Hey guys. A new question: I want to use an STL libarary to hold a bunch of objects I create. Actually, it will hold references to the objects, but that's beside the point, for the most part. ...
13
1735
by: Alvin | last post by:
Correct me if I am wrong, but the STL standard only defines the interface only and not the implementation, right? For example, say I use a std::map. The speed and efficiency of the operator(key)...
35
2586
by: Jon Slaughter | last post by:
I'm having a problem allocating some elements of a vector then deleting them. Basicaly I have something like this: class base { private: std::vector<object> V;
11
4724
by: ma740988 | last post by:
I'm perusing a slide with roughly 12 bullets spread across 3 pages. Each bullet reflects 'advice'. I'm ok with all but 1 bullet, more specifically the bullet that states: " Avoid the STL unless...
8
6154
by: olanglois | last post by:
Hi, I was asking myself to following question. What is better to erase an element from a STL map: calling (option #1) size_type erase(const key_type& k) or calling (option #2)
48
2582
by: Tony | last post by:
How much bloat does the STL produce? Is it a good design wrt code bloat? Do implementations vary much? Tony
1
3053
by: krunalbauskar | last post by:
Hi, Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. For example - <snippet> #include <iostream> #include <vector>
5
4894
by: feverzsj | last post by:
STL sort() as an implementation of introsort, below is the code snippet from the gnu stl. template<typename _RandomAccessIterator, typename _Size> void __introsort_loop(_RandomAccessIterator...
0
7105
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,...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7132
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,...
0
7180
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...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5439
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,...
1
4870
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...
0
1381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
600
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.