c++ compiled program much slower than g++

Thread Solved

Join Date: Aug 2008
Posts: 4
Reputation: mabuse is an unknown quantity at this point 
Solved Threads: 0
mabuse mabuse is offline Offline
Newbie Poster

c++ compiled program much slower than g++

 
0
  #1
Aug 27th, 2008
The same source code is compiled with g++ (linux) and with visual c++. I found that the program runs much slower on the second one (0.03 seconds vs 0.359 seconds).

The program uses <vector> and does a convolution:

  1. void DSP_fir_gen1
  2. (
  3. vector < double > *x,
  4. double *h,
  5. vector < double > *r,
  6. int nh,
  7. int nr
  8. )
  9. {
  10. int i, j;
  11. double sum;
  12. (*r).clear();
  13. for (j = 0; j < nr; j++)
  14. {
  15. sum = 0;
  16. for (i = 0; i < nh; i++)
  17. {
  18. sum += (*x)[i + j] * h[i];
  19. }
  20. (*r).push_back(sum);
  21. }
  22. }

The aprox. "x" width is about 36656 doubles and "h" is between 2 and 25. I don't understand why the program compiled in visual c++ 6 is so painfully slow.
When I compile in linux I use the options:
  1. -O0 -g3 -Wall -c -fmessage-length=0

And in visual c++ 6.0:
  1. kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /incremental:yes /pdb:"Debug/QRSdetectMain.pdb" /debug /machine:I386 /out:"Debug/QRSdetectMain.exe" /pdbtype:sept

My PC is a Pentium 4 intel 3.2 Ghz 3.2 Ghz 512 Mb RAM.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,979
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 220
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: c++ compiled program much slower than g++

 
0
  #2
Aug 27th, 2008
I would suspect all that debug code in the VC++ options, and it doesn't look like you have many optimizations enabled either...

Go to your project options and disable debug info and enable all the optimizations you can, and see if that doesn't make a difference.

(I still can't get VC++ to install on my machine... so I can't help more... I just updated to SP3 so maybe I'll get that pesky .NET 3 to work now.)

Good luck!
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 6,601
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 852
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: c++ compiled program much slower than g++

 
0
  #3
Aug 27th, 2008
A couple of things.
1. VC6 is probably 10 years older than your Linux compiler.

2. Have you considered using reserve() to allocate all the space up front, then just subscript your accesses (rather than push_back)
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
--
If your code lacks code tags, you will be IGNORED
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 4
Reputation: mabuse is an unknown quantity at this point 
Solved Threads: 0
mabuse mabuse is offline Offline
Newbie Poster

Re: c++ compiled program much slower than g++

 
0
  #4
Aug 27th, 2008
Originally Posted by Salem View Post
A couple of things.
1. VC6 is probably 10 years older than your Linux compiler.

2. Have you considered using reserve() to allocate all the space up front, then just subscript your accesses (rather than push_back)
1. I have considered it, but I believed that It shouldn't be so important. Do you think that an old compilator can differ a magnitude order from the new one?

2. You are right! this time I can use reserve without problems because I know the size before. It can improve slightly the speed. thanks.86

There is something I'm not sure about:

My goal is to do a dll library in windows OS -and I am not used to work in windows-. Can I do easily a dll for windows without using Microsoft Visual C++ compilator?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 8,313
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 824
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: c++ compiled program much slower than g++

 
1
  #5
Aug 27th, 2008
>Do you think that an old compilator can differ a magnitude order from the new one?
Yes. Especially since Visual C++ 6.0's STL implementation was extremely weak.

>Can I do easily a dll for windows without using Microsoft Visual C++ compilator?
Writing DLLs in Windows is not difficult. Though I would highly recommend upgrading your Visual C++ to 2005 or 2008. Visual C++ 6.0 is painful if you plan on making use of templates.
Last edited by Narue; Aug 27th, 2008 at 4:36 pm.
In case you were wondering, yes, I do hate you.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,608
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1614
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: c++ compiled program much slower than g++

 
0
  #6
Aug 27th, 2008
>>Can I do easily a dll for windows without using Microsoft Visual C++ compiler ?
yes, you can use most any compiler that targets MS-Windows version 2000 or later.
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 6,601
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 852
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: c++ compiled program much slower than g++

 
0
  #7
Aug 27th, 2008
Since there is virtually nothing in common (different compiler, different operating systems, different eras), it's very hard to make an objective comparison based solely on one small test of one aspect of one feature of the STL.

It might be an order of magnitude, but it's still the blink of an eye (in human perception terms).

Also, the STL only makes statements about complexity, not performance. If you try your test with 3000 elements and 300,000 elements, you might get a different ratio of performance.

> Do you think that an old compilator can differ a magnitude order from the new one?
Apparently, it can. More inline code, better optimisation and a better allocation strategy - yeah, perhaps it could.


As for your last question, Visual Studio Express is
a) far more up to date
b) far more standard compliant
c) $0 in price.
Last edited by Salem; Aug 27th, 2008 at 4:49 pm.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
--
If your code lacks code tags, you will be IGNORED
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 4
Reputation: mabuse is an unknown quantity at this point 
Solved Threads: 0
mabuse mabuse is offline Offline
Newbie Poster

Re: c++ compiled program much slower than g++

 
0
  #8
Aug 27th, 2008
Thank you to all!
I usually work under linux and It's my first time I have to make a dll library under windows. I will test all alternatives you posted and post my results.
Anyway, thanks again!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 344
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: c++ compiled program much slower than g++

 
0
  #9
Aug 28th, 2008
Because of this thread is not closed yet, allow me to add some remarks.
On my AMD 5000+ (~2.6 MHz) with VC++ 2008 (release, optimize for speed) this function runs ~3.8 milliseconds (data sizes are 36656 and 25).
It's interesting that a variant of this function with std::valarray<double> data runs ~1.3 milliseconds only:
  1. void DSP_fir_gen1v
  2. (
  3. valarray <double>& x,
  4. valarray <double>& h,
  5. valarray <double>& r,
  6. int nh,
  7. int nr
  8. )
  9. {
  10. double sum;
  11. //(*r).clear();
  12. for (int j = 0; j < nr; j++)
  13. {
  14. sum = 0;
  15. for (int i = 0; i < nh; i++)
  16. {
  17. sum += x[i + j] * h[i];
  18. }
  19. r[j] = sum;
  20. //(*r).push_back(sum);
  21. }
  22. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,608
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1614
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: c++ compiled program much slower than g++

 
0
  #10
Aug 28th, 2008
>>Because of this thread is not closed yet
We don't close threads unless we have to normally when someone starts behaving badly or flaming starts.

Please post the whole program so we can run it and compare execution times with different computers.
Last edited by Ancient Dragon; Aug 28th, 2008 at 5:29 pm.
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 1035 | Replies: 11
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC