1

I have a c++ application, which contains large amount of std::cout. It runs on linux 2.6.x. I need to test the performance of the application, so i am thinking of redirecting the std::cout to /dev/null. In C, i could simply use dup2. Is there an equivalent in c++ to redirect std::ostream to a file or /dev/null?

4
  • Note, I/O is usually one of the biggest bottlenecks. If you start messing with that, can you really trust your numbers? Commented Feb 13, 2013 at 2:32
  • to effectively test the performance, you should really refactor the running code and separate it out of the i/o code. Probably make it a function..such that the function should not care where the data came from (either i/o or hard-coded) Commented Feb 13, 2013 at 2:36
  • possible duplicate of freopen() equivalent for c++ streams Commented Feb 13, 2013 at 3:28
  • A note for Windows programmers: dup2 method from Potatoswatter works like a charm. Commented Jun 11, 2013 at 15:11

3 Answers 3

6

The dup2 trick will still work in C++, since just like <stdio.h>, <iostream> is just a buffering layer atop the UNIX system calls.

You can also do this at the C++ level by disconnecting the buffer from std::cout:

std::cout.rdbuf( NULL ); 

Besides severing the relationship between std::cout and any actual output device, this will set the std::ios::badbit flag which will prevent any output conversions (e.g. numbers to text) from occuring. Performance should be much better than with the filesystem-level hack.

Sign up to request clarification or add additional context in comments.

7 Comments

+1 for an informative post, but re "I need to test the performance of the application" / "Performance should be much better than with the filesystem-level hack." - it could be argued that avoiding conversions is no longer testing the application performance.
@TonyD Asking how to test performance is a loaded question. Performance is tested by carefully replicating real-world conditions, and nobody here can answer that. I'm assuming he wants to test how it would perform if it weren't for all the output. If that's not a practical use, then the question is pointless.
This is really good answer. A quick Question, can you suggest some resource to learn more about iostreams?
@Jimm I don't personally use any resources besides the ISO standard document. But there's always cppreference.com and I have this in my bookmarks: horstmann.com/cpp/streams.txt
@Potatoswatter: in my experience, it's common to want to know the performance including the generation of output, but without the time taken by the I/O device to write. For example, to be able to say things like "on that spec CPU/ram etc. it'll take 10 second + however long your disk needs to write the output", or "generation of output took 12 seconds, but when writing that to disk it took 40 seconds overall - how about we get a SSD?".
|
3

You can do the exact same thing in C++. Both C and C++ both rely on the underlying operating system for IO, and redirecting fd 1 will affect std::cout just like it affects stdout.

(of course for testing you can just run the command with > /dev/null on the command line...)

Comments

0

An alternative way would be to symlink your file to /dev/null.

% ln -s /dev/null core % ls -l core lrwx-xr-x 1 john users 9 Nov 18 12:26 core -> /dev/null 

To truly test your program speed however I would suggest to comment out the writes to your file and calculate the execution time difference, because writing to /dev/null might have different overhead than writing to a normal file.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.