Odd results with C++ under WinXP SP2

A

Anonymous

I have written a series of applications in different languages to compare
them. In my first one, I am testing console output. On my first test OS
(Mandrake Linux), C++ came in second, just behind Perl. However, on Windows
XP, C++ is falling behind badly. I am compiling using the Microsoft Visual
C++ Toolkit 2003 Compiler, and providing the /O2 option to compile for
speed. Still, the others are easily doing the task three times as
effeciently as C++. Perhaps I have made an error (should I be using a
different compiler? did I make a coding error?). Any advice would be very
enlightening.

My test languages are: C++, Java, Perl, Python, & Ruby. On Linux, Perl came
in ahead of C++, but all others behind. Here is my source code:

**C++**
//Raw Console Output - C++ test
#include <iostream>
int main() {
std::cout << "C++ Raw Output Test Commencing...\n\n";
for(unsigned long i=0; i<200000; i++) {
std::cout << "Count: " << i << "\n";
}
std::cout << "C++ Raw Output Test Complete.\n\n";
}

**Java**
//Raw Console Output - Java test
public class consoleoutput {
public static void main(String[] args) {
System.out.print("Java Raw Output Test Commencing...\n\n");
for(long i=0; i<200000; i++) {
System.out.println("Count: " + i);
}
System.out.print("Java Raw Output Test Complete.\n\n");
}
}


**Python**
#!/usr/bin/python
#Raw Console Output - Python Test
print 'Python Raw Output Test Commencing...\n'
i=0
while i<200000:
print 'Count: %d' % i
i=i+1

(in Python, those spaces actually are tabs in my file)

I will provide my other source code if required.
To be precise, C++ is using all of the CPU, and taking 56.48 sec.
Java is using 46% of the CPU, and taking 36.64 sec.
Python is using 65% of the CPU, and taking 37 sec.
I am using Windows XP Home SP2, with 1.8 GHz Pentium 4 Processor, and 256 MB
RAM.

--
Anonymous

"Treat your password like your toothbrush.
Don't let anybody else use it, and get
a new one every six months."
---Clifford Stoll
 
V

Victor Bazarov

Anonymous said:
I have written a series of applications in different languages to compare
them. In my first one, I am testing console output. [...]
Perhaps I have made an error (should I be using a different compiler? did
I make a coding error?)[..]

You didn't make a mistake yet, AFAICS. However, you're probably about
to make one. The mistake will be to derive any conclusion from your
"comparisons".

V
 
D

Donovan Rebbechi

I have written a series of applications in different languages to compare
them.

Compare what, the applications or the languages ? If you think you're
comparing the languages in a meaningful way, there's your first mistake right
there.

As far as IO performance is concerned, buffering is an issue -- I remember the
language shootout page using some obscure buffer that is seldom used outside
benchmarks (go figure) setting to get the C++ program to perform faster. BTW,
if you're trying to make the program run as slowly as possible, use std::cin
as well as std::cout. Otherwise, try doing away with iostreams.

Most valid C programs are also valid C++, so if the C program beats C++ by a
wide margin, that tells you that you could have written a faster C++ program.

Cheers,
 
L

Llewelly

Anonymous said:
I have written a series of applications in different languages to compare
them. In my first one, I am testing console output. On my first test OS
(Mandrake Linux), C++ came in second, just behind Perl. However, on Windows
XP, C++ is falling behind badly. I am compiling using the Microsoft Visual
C++ Toolkit 2003 Compiler, and providing the /O2 option to compile for
speed. Still, the others are easily doing the task three times as
effeciently as C++. Perhaps I have made an error (should I be using a
different compiler? did I make a coding error?). Any advice would be very
enlightening.

My test languages are: C++, Java, Perl, Python, & Ruby. On Linux, Perl came
in ahead of C++, but all others behind. Here is my source code:

**C++**
//Raw Console Output - C++ test
#include <iostream>
int main() {
std::cout << "C++ Raw Output Test Commencing...\n\n";
for(unsigned long i=0; i<200000; i++) {
std::cout << "Count: " << i << "\n";

This isn't raw output at all! In C++, the << operators do _formatted_
i/o, not raw.

For raw i/o, you must use a streambuf.
}
std::cout << "C++ Raw Output Test Complete.\n\n";
}
[snip]
 
I

Ioannis Vranos

Anonymous said:
I have written a series of applications in different languages to compare
them. In my first one, I am testing console output. On my first test OS
(Mandrake Linux), C++ came in second, just behind Perl. However, on Windows
XP, C++ is falling behind badly. I am compiling using the Microsoft Visual
C++ Toolkit 2003 Compiler, and providing the /O2 option to compile for
speed. Still, the others are easily doing the task three times as
effeciently as C++. Perhaps I have made an error (should I be using a
different compiler? did I make a coding error?). Any advice would be very
enlightening.

My test languages are: C++, Java, Perl, Python, & Ruby. On Linux, Perl came
in ahead of C++, but all others behind. Here is my source code:

**C++**
//Raw Console Output - C++ test
#include <iostream>
int main() {
std::cout << "C++ Raw Output Test Commencing...\n\n";
for(unsigned long i=0; i<200000; i++) {
std::cout << "Count: " << i << "\n";
}
std::cout << "C++ Raw Output Test Complete.\n\n";
}



First is the fact that 256 MB are not enough for XP SP2 and it is
certain that paging file operations occur affecting benchmarks.

Secondly you should increase the buffer of cout since you are interested
in console output benchmarks.

Third you should use the following switches:

cl /O2 /Og /Oi /Oy /GA /G7 /arch:SSE2 /EHsc temp.cpp


Finally so as to avoid buffering optimisation issues, a more reliable
benchmark would be non-console operations like sorting of arrays.


#include <valarray>
#include <algorithm>


int main()
{
using namespace std;

// 20 MB
const unsigned long SIZE=20*1024*1024;

valarray<int> array(SIZE);


for(unsigned long i=0; i<array.size(); ++i)
array= array.size()-i;

sort(&array[0], &array[0]+array.size());
}


Again use the provided command line options.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top