c++ optimization

  • Thread starter kasthurirangan.balaji
  • Start date
K

kasthurirangan.balaji

Hi,

Below is the code.

#include <fstream>
#include <set>
#include <string>
#include <iterator>
#include <algorithm>

main()
{
typedef std::set<std::string> stringset;
stringset list;
std::ifstream ifstr("data.dat");
std::copy(std::istream_iterator<std::string>(ifstr),
std::istream_iterator<std::string>(),
std::inserter(list,list.begin()));
}

If i compile with "-O3" optimization option, what would be the actual
optimization?? Since i read everything(line by line) into memory that
is a binary tree(c++ set), would be number of disk reads be reduced??
Would the file be mapped into memory directly and then into set?? I
probably see this depends on the compiler and implementation, but like
to have more idea of what happens in general.

Thanks,
Balaji.
 
I

Ian Collins

Hi,

Below is the code.

#include <fstream>
#include <set>
#include <string>
#include <iterator>
#include <algorithm>

main()
{
typedef std::set<std::string> stringset;
stringset list;
std::ifstream ifstr("data.dat");
std::copy(std::istream_iterator<std::string>(ifstr),
std::istream_iterator<std::string>(),
std::inserter(list,list.begin()));
}

If i compile with "-O3" optimization option, what would be the actual
optimization?? Since i read everything(line by line) into memory that
is a binary tree(c++ set), would be number of disk reads be reduced??
Would the file be mapped into memory directly and then into set?? I
probably see this depends on the compiler and implementation, but like
to have more idea of what happens in general.
The best places to find the answers are your compiler documentation
(yes, it really is that specific) and looking through the generated
assembly code.
 
J

James Kanze

Below is the code.
#include <fstream>
#include <set>
#include <string>
#include <iterator>
#include <algorithm>

You're missing the return type for main. This isn't legal C++.
{
typedef std::set<std::string> stringset;
stringset list;
std::ifstream ifstr("data.dat");
std::copy(std::istream_iterator<std::string>(ifstr),
std::istream_iterator<std::string>(),
std::inserter(list,list.begin()));
}
If i compile with "-O3" optimization option, what would be the
actual optimization??

Whatever the compiler decides to give you. Supposing it even
understands -O3 as a request for optimization.
Since i read everything(line by line) into memory that is a
binary tree(c++ set), would be number of disk reads be
reduced??

Almost certainly not. Compilers don't normally optimize disk
accesses. The number of accesses will probably depend on the
implementation of std::filebuf in the standard library.

And the code you show doesn't read line by line, but word by
word.
Would the file be mapped into memory directly and then into
set??

It might be, but in that case, it would probably be memory
mapped without optimization as well. It's not necessarily
certain that memory mapping would improve performance, either.
(Obviously, the data will have to be copied into the set,
regardless of you it is read.)

If the data in input is already sorted, using std;:inserter(
list, list.end() ) might be better; using an explicit loop with
insert( newValue, list.end() ) each time could also be faster.

Benchmark it.
I probably see this depends on the compiler and
implementation, but like to have more idea of what happens in
general.

In general, the compiler will try to optimize the code it
generates. It can't do much with regards to disk accesses.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top