473,396 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

What influences C++ I/O performance?

SzH

I would like to read in large integer matrices from text files, as
quickly as possible. I did a naive implementation (see below), but
for an input file with 100 rows and 250000 columns it runs more than
5x slower when compiled with visual studio express 2008 than the
version compiled with gcc 4.2.1 (mingw), on the same computer, same
OS.

I am not a regular C++ user. What are the things (relevant to the
standard C++ library) that influence the performance of I/O? I don't
believe that this is a "bug" in the VS compiler. It is more likely
that there are some relevant settings for iostreams (with a
significant impact on performance) that I am not aware of.

Any suggestions for making this program perform well portably would me
most welcome!

This is the relevant portion of the code:

std::ios::sync_with_stdio(false);
std::ifstream infile(path);

input_array arr(); // see note below

int row = 0, max_col = 0;

std::string line;
while (std::getline(infile, line)) {
std::istringstream ln(line);
int number;
int col = 0;
while (ln >number) {
arr.append(number);
col++;
}
if (col != 0) {
if (row == 0)
max_col = col;
if (col != max_col) {
/*
* Array is not rectangular.
* Handle error (exit).
*/
}
row++;
}
}

Notes: 'input_array' is just a simple integer array, which grows
exponentially to accommodate new elements. It is not the source of
the performance problem.

Szabolcs
Feb 3 '08 #1
8 1957
On 3 helmi, 16:04, SzH <szhor...@gmail.comwrote:
I am not a regular C++ user. What are the things (relevant to the
standard C++ library) that influence the performance of I/O?
Compiler optimization settings could be worth trying.
I guess VS has slower debug mode executables as well.
Feb 3 '08 #2
SzH
On Feb 3, 5:09 pm, Krice <pau...@mbnet.fiwrote:
On 3 helmi, 16:04, SzH <szhor...@gmail.comwrote:
I am not a regular C++ user. What are the things (relevant to the
standard C++ library) that influence the performance of I/O?

Compiler optimization settings could be worth trying.
I guess VS has slower debug mode executables as well.
I forgot to mention that "release" mode was used with VS
(optimizations turned on). In "debug" mode, the program is twice as
slow, i.e. 10x as slow as with gcc. Whether optimizations are turned
on or not didn't really matter with gcc.

The VS timing was approx 80 seconds (in "release" mode), while the gcc
timing was about 15 seconds.
Feb 3 '08 #3
On Sun, 17 Feb 2008 05:05:47 -0800 (PST), SzH wrote:
>How do I read the numbers without the istringstream? But
std::istringstream isn't the source of the "slowness" anyway. The
piece that takes a long time to execute is

while (ln >number)
col++;
This sentence seems to contain a contradiction. Anyway. AFAICS, you
have a string of ints separated by some whitespace. You could try
something like the following to extract the ints (no predicitons about
performance are provided). Error handling is omitted like in the
original code.

int main() {
using namespace std;

string str = "1 314 1024 ";

char* current = const_cast<char*(str.c_str()) ;

while(true) {
const char* start = current;
long result = strtol(start, &current, 0);
if(start == current) {
break;
}
cout << result << " ";
}
}
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Feb 17 '08 #4
SzH
On Feb 17, 5:06 pm, rpbg...@yahoo.com (Roland Pibinger) wrote:
On Sun, 17 Feb 2008 05:05:47 -0800 (PST), SzH wrote:
How do I read the numbers without the istringstream? But
std::istringstream isn't the source of the "slowness" anyway. The
piece that takes a long time to execute is
while (ln >number)
col++;

This sentence seems to contain a contradiction.
Yes. I should have said that *constructing* the istringstream is not
the problem.
Anyway. AFAICS, you
have a string of ints separated by some whitespace. You could try
something like the following to extract the ints (no predicitons about
performance are provided).
That would be hard to do with these unpredictable libraries ... but
fortunately it is a lot faster :) Thanks!

Timings are

vs: 10.531
gcc: 4.953

compared to the previous

vs: 51.165
gcc: 8.795
Error handling is omitted like in the
original code.

int main() {
using namespace std;

string str = "1 314 1024 ";

char* current = const_cast<char*(str.c_str()) ;

while(true) {
const char* start = current;
long result = strtol(start, &current, 0);
if(start == current) {
break;
}
cout << result << " ";
}

}

--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Feb 17 '08 #5
In article <98cd0ba4-cfc5-4052-8a09-07b7d1b6a7da@
41g2000hsc.googlegroups.com>, sz******@gmail.com says...

[ ... ]
Yes, that's the very disappointing thing about C++. One can never
rely on the damn standard library for good performance! You just
switch compilers, and suddenly find that a stupid scripting language
performs much better than your C++ program.
In this case, it's really a matter of the nature of templates -- since
their behavior can change substantially when instantiated over a
different type, it's not really possible to pre-compile a template to
object code.

That means if you use standard library (or almost any other) code that's
based on templates (which includes most of the C++ standard library),
all that code is being compiled as part of your program. When, as in
your case, you use a compiler from which nearly all optimization
capability has been removed, the result can (obviously) be pretty ugly.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Feb 17 '08 #6
On 2008-02-17 19:48, Jerry Coffin wrote:
In article <0ab81bd1-1e5a-43eb-8a7f-bfc9d0148d5c@
28g2000hsw.googlegroups.com>, sz******@gmail.com says...

[ ... ]
1) the newer version of VC++ has reduced I/O speed a lot.

I only have VS 2008 Express, so I don't know, but it seems possible.

If you previously said you were using the "Express" edition, I didn't
catch it.

In any case, that's almost certainly the majority of the explanation
right there. Much of what they do in the Express edition of the compiler
is disable nearly _all_ optimization. Since iostreams are templates,
that means most of the code involved is being compiled with virtually no
optimization applied.

Fortunately, this is fairly easy to fix: the full version of the
compiler is included with (among other things) a number of versions of
the Windows SDK. If you care about performance at all, I'd advise
downloading and using that (at least for final builds).

IOW, I suspect (quite strongly) that the difference we're seeing IS
between different version of the compiler; the difference between 7.1
and 8.0 or 9.0 is probably NOT significant, but the difference between
the full, optimizing version of the compiler and the "Express" version
probably is.
Actually the compiler in the Express editions are exactly the same as in
the other editions, the differences are the libraries (MFC etc.) and
tools that are included. Perhaps there are some differences in the flags
that are used by default but that is easily fixed.

--
Erik Wikström
Feb 17 '08 #7
On 2008-02-17 19:48, Jerry Coffin wrote:
In article <0ab81bd1-1e5a-43eb-8a7f-bfc9d0148d5c@
28g2000hsw.googlegroups.com>, sz******@gmail.com says...

[ ... ]
1) the newer version of VC++ has reduced I/O speed a lot.

I only have VS 2008 Express, so I don't know, but it seems possible.

If you previously said you were using the "Express" edition, I didn't
catch it.

In any case, that's almost certainly the majority of the explanation
right there. Much of what they do in the Express edition of the compiler
is disable nearly _all_ optimization. Since iostreams are templates,
that means most of the code involved is being compiled with virtually no
optimization applied.

Fortunately, this is fairly easy to fix: the full version of the
compiler is included with (among other things) a number of versions of
the Windows SDK. If you care about performance at all, I'd advise
downloading and using that (at least for final builds).
I forgot to add, the Windows Platform SDK comes as a part of Visual C++
2008 Express, so downloading it again will buy you nothing.

--
Erik Wikström
Feb 17 '08 #8
In article <ff5e51c0-c004-43bd-9c02-ef6ef7d6a098
@p73g2000hsd.googlegroups.com>, sz******@gmail.com says...

[ ... ]
You know, it's just terribly disappointing and frustrating when one
finds out that even a stupid scripting language outperforms one's C++
implementation.
I'd be rather happy with that. Scripting languages tend to make
development and maintenance cheap and easy, so when they can do the job,
it's a great thing.

Most scripting languages are (at least partially) interpreted, so
execution speed tends to depend heavily upon the amount of code that
needs to be executed. When/if you can get the job done in only a line or
two of code, they'll typically be as fast as (and perhaps faster than)
anything you can reasonably write yourself. In particular, the code that
implements those few lines of code is likely to be used enough to
justify spending quite a bit of time and effort on optimizing them.

Chances are that when you recreate that functionality on your own that
you can't justify spending nearly as much time and effort on optimizing
this code, so you may not get a lot faster result.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Feb 17 '08 #9

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

Similar topics

54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
14
by: ketulp_baroda | last post by:
i am developing a web application and i am really confused on what should i use. should i use just python and use the cgi module availabe. Or should i use application like WebWare.Also there is PSP...
47
by: Neal | last post by:
Patrick Griffiths weighs in on the CSS vs table layout debate in his blog entry "Tables my ass" - http://www.htmldog.com/ptg/archives/000049.php . A quite good article.
20
by: Steven T. Hatton | last post by:
I just read this in the description of how C++ is supposed to be implemented: "All external object and function references are resolved. Library components are linked to satisfy external...
86
by: Randy Yates | last post by:
In Harbison and Steele's text (fourth edition, p.111) it is stated, The C language does not specify the range of integers that the integral types will represent, except ot say that type int may...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
7
by: Banaticus | last post by:
http://www.banaticus.com/test/neighborhoods.xml I've tried validating it in several online validators but either the validation just doesn't work or I get conflicting errors. The document works...
10
by: JoeC | last post by:
I have been programming for a while and I have seen this syntax before and I copied this from a book but the book didn't explain what is going on here. class engine{ protected: static engine*...
10
by: timor.super | last post by:
Hi all, Imagine I've an array of int : int anArray = new int; I want to extract all the integer that are superior to 500 I can do :
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.