Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

problem with microsoft speed optimization

0 views
Skip to first unread message

mjbackues at yahoo

unread,
Dec 29, 2005, 9:59:54 AM12/29/05
to
Hello.

I'm having a problem with the Visual Studio .net (2003) C++ speed
optimization, and hope someone can suggest a workaround.

My project includes many C++ files, most of which work fine with speed
optimization turned on. At least one does not however, though it does
work with size optimization turned on. I don't know specifically what
the optimizer is doing wrong, just that the output is incorrect. And I
know within about 10 lines where the problem is. I've tried
rearranging the offending lines (which are straight C code). I've
tried using 'volitle' data types in hopes of avoiding optimization.
I've looked for missing variable initializations, but everything looks
correct and straightforward.

One solution would be to have a precompiler directive which would
enable (or disable) the optimization only on specific files. Of course
I can manually turn the optimization on and off while recompiling
specific files, but I need something automated.

I can't post the code, but will try to follow up with a test case that
exhibits the same behavior.

Presumably the program would compile fine with g++, but unfortunately
that's not an option for this project.

Thanks very much.

Mark

deane...@hotmail.com

unread,
Dec 29, 2005, 10:05:26 AM12/29/05
to

It sounds like you're asking about the particular behaviour of your
compiler's optimiser. If so, then you are asking the wrong group. There
is a VC++ .Net group suggested here.

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.9

Gavin Deane

mjbackues at yahoo

unread,
Dec 29, 2005, 10:15:52 AM12/29/05
to
OK, thanks.

mlimber

unread,
Dec 29, 2005, 10:18:15 AM12/29/05
to
mjbackues at yahoo wrote:
> Hello.
>
> I'm having a problem with the Visual Studio .net (2003) C++ speed
> optimization, and hope someone can suggest a workaround.

This subject matter is off-topic in this group, which is solely
concerned with *standard* C++. See the FAQ for what is on-topic and for
some suggestions of better places to post:

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.9

> My project includes many C++ files, most of which work fine with speed
> optimization turned on. At least one does not however, though it does
> work with size optimization turned on. I don't know specifically what
> the optimizer is doing wrong, just that the output is incorrect. And I
> know within about 10 lines where the problem is. I've tried
> rearranging the offending lines (which are straight C code). I've
> tried using 'volitle' data types in hopes of avoiding optimization.
> I've looked for missing variable initializations, but everything looks
> correct and straightforward.

People often blame the compiler for strange errors, but it usually
turns out to be user error, perhaps in an apparently unrelated part of
the program. Have you tried looking at the generated assembly code?

> One solution would be to have a precompiler directive which would
> enable (or disable) the optimization only on specific files. Of course
> I can manually turn the optimization on and off while recompiling
> specific files, but I need something automated.

Look up #pragma in your compiler documentation. Better yet, turn off
the optimizer altogether (except for inlining and such). See this
article, "Optimization: Your Worst Enemy":

http://www.codeproject.com/tips/optimizationenemy.asp

See also "Surviving the Release Version":

http://www.codeproject.com/debug/survivereleasever.asp

> I can't post the code, but will try to follow up with a test case that
> exhibits the same behavior.

You should have done this before posting. See the FAQ:

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

But, of course, if you had read the FAQ in advance, you would have
posted to a different newsgroup to begin with. ;-)

> Presumably the program would compile fine with g++, but unfortunately
> that's not an option for this project.

Such warantless speculation is entirely unhelpful. If possible, you
should try at least the problem spot in your code on that and any other
compiler you have at your disposal. You could use the same distilled
code that you'll be posting shortly, and that might generate some
helpful results.

Cheers! --M

mjbackues at yahoo

unread,
Dec 29, 2005, 11:15:34 AM12/29/05
to
Wow. Since you fancy yourself clever and obviously have time on your
hands....following is a test program that works with gcc but not with
visual studio (either with or without optimization). The output using
'fprintf' is correct, whereas the output with 'fwrite' is not, and the
resulting file doesn't even have the right size. The 'fwrite' does
work however if the '3.14' in the preceeding calculation is replaced
with another pi approximation with more digits.

Useful responses from other people are also welcome.

#include <stdio.h>
#include <math.h>
#include <malloc.h>
#define PI 3.14159265358979

main(){
float* ph;
int i;
FILE *fid;

ph = (float*)malloc(sizeof(float)*128*1024*2);

for(i=0;i<128*1024*2;i+=2){
ph[i]=(float)cos(3.14*(double)i*0.2);
ph[i+1]=1.0;
}

fid=fopen("out1","w");
for(i=0;i<128*1024*2;i++)
fprintf(fid,"%lf\n",ph[i]);
fclose(fid);

fid = fopen("out2","w");
fwrite(ph,sizeof(float),128*1024*2,fid);
fclose(fid);

free(ph);
}

Pete Becker

unread,
Dec 29, 2005, 11:19:53 AM12/29/05
to
mjbackues at yahoo wrote:
> fid = fopen("out2","w");
> fwrite(ph,sizeof(float),128*1024*2,fid);

When you're writing binary data you must open the file with "wb" rather
than "w".

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

mlimber

unread,
Dec 29, 2005, 11:49:36 AM12/29/05
to

In what way is the output incorrect?

BTW, a more C++-style way to do it might look something like:

#include <vector>
#include <cmath>
#include <fstream>
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;

#define PI 3.14159265358979

int main()
{
vector<double> v( 128*1024*2 );

for(unsigned int i=0; i < v.size(); i+=2 )
{
v[i]=cos(PI*i*0.2);
v[i+1]=1.0;
}

ofstream f1("out1");
if( !f1 )
{
cerr << "error!" << endl;
return -1;
}
copy( v.begin(), v.end(),
ostream_iterator<double>( f1, "\n" ) );

ofstream f2( "out2", ios_base::binary );
if( !f2
|| !f2.write( reinterpret_cast<const char*>( &v[0] ),
v.size()*sizeof(double) ) )
{
cerr << "error!" << endl;
return -1;
}

return 0;
}

mjbackues at yahoo

unread,
Dec 29, 2005, 12:07:41 PM12/29/05
to
yeah, that's it, thanks very much.

mjbackues at yahoo

unread,
Dec 29, 2005, 12:16:26 PM12/29/05
to
Thanks. This was a problem a coworker had, and he wound up using
ofstream and write to get around it.

mjbackues at yahoo

unread,
Dec 29, 2005, 12:18:40 PM12/29/05
to
>yeah, that's it, thanks very much.

oops, intended as reply to Pete Becker

E. Mark Ping

unread,
Dec 29, 2005, 12:53:06 PM12/29/05
to
In article <t5SdnbmlSeM7kSne...@giganews.com>,

Pete Becker <peteb...@acm.org> wrote:
>mjbackues at yahoo wrote:
>> fid = fopen("out2","w");
>> fwrite(ph,sizeof(float),128*1024*2,fid);
>
>When you're writing binary data you must open the file with "wb" rather
>than "w".

And IIRC the default mode is text in VC, but binary in GCC. Which
caused some interesting issues when I was porting some code a while
back.
--
Mark Ping
ema...@soda.CSUA.Berkeley.EDU

Mike Smith

unread,
Dec 29, 2005, 3:32:42 PM12/29/05
to
mjbackues at yahoo wrote:
>
> One solution would be to have a precompiler directive which would
> enable (or disable) the optimization only on specific files. Of course
> I can manually turn the optimization on and off while recompiling
> specific files, but I need something automated.

IIRC there are #pragmas that can do this; consult your documentation.

--
Mike Smith

Jack Klein

unread,
Dec 29, 2005, 4:35:56 PM12/29/05
to
On Thu, 29 Dec 2005 17:53:06 +0000 (UTC),
ema...@soda.csua.berkeley.edu (E. Mark Ping) wrote in comp.lang.c++:

> In article <t5SdnbmlSeM7kSne...@giganews.com>,
> Pete Becker <peteb...@acm.org> wrote:
> >mjbackues at yahoo wrote:
> >> fid = fopen("out2","w");
> >> fwrite(ph,sizeof(float),128*1024*2,fid);
> >
> >When you're writing binary data you must open the file with "wb" rather
> >than "w".
>
> And IIRC the default mode is text in VC, but binary in GCC. Which
> caused some interesting issues when I was porting some code a while
> back.

The default mode is text in any conforming C or C++ compiler. It just
so happens that on *X operating systems, there is no translation or
substitution of characters in text mode, so the effect of using text
or binary mode is the same.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

mjbackues at yahoo

unread,
Dec 29, 2005, 5:17:33 PM12/29/05
to
Thanks. #pragmas didn't work, but someone pointed out some file
specific settings that did.

Pete Becker

unread,
Dec 30, 2005, 4:46:40 AM12/30/05
to
E. Mark Ping wrote:
>
> And IIRC the default mode is text in VC, but binary in GCC. Which
> caused some interesting issues when I was porting some code a while
> back.

When passed to fopen, "w" says to open for writing in text mode. "wb"
says to open for writing in binary mode. VC and GCC both implement that
correctly.

E. Mark Ping

unread,
Dec 30, 2005, 12:44:37 PM12/30/05
to
In article <r8mdndAn6IR...@giganews.com>,

Pete Becker <peteb...@acm.org> wrote:
>E. Mark Ping wrote:
>>
>> And IIRC the default mode is text in VC, but binary in GCC. Which
>> caused some interesting issues when I was porting some code a while
>> back.
>
>When passed to fopen, "w" says to open for writing in text mode. "wb"
>says to open for writing in binary mode. VC and GCC both implement that
>correctly.

That may be the case now, but for GCC it wasn't on the version I was
porting from in 2002. Now that I think about it, it was on a read,
not a write.
--
Mark Ping
ema...@soda.CSUA.Berkeley.EDU

mlimber

unread,
Dec 30, 2005, 12:59:01 PM12/30/05
to
E. Mark Ping wrote:
> In article <r8mdndAn6IR...@giganews.com>,
> Pete Becker <peteb...@acm.org> wrote:
> >E. Mark Ping wrote:
> >>
> >> And IIRC the default mode is text in VC, but binary in GCC. Which
> >> caused some interesting issues when I was porting some code a while
> >> back.
> >
> >When passed to fopen, "w" says to open for writing in text mode. "wb"
> >says to open for writing in binary mode. VC and GCC both implement that
> >correctly.
>
> That may be the case now, but for GCC it wasn't on the version I was
> porting from in 2002.
[snip]

It may well have been just an OS dependency, not a non-conformancy in
the compiler. See Jack Klein's post:

http://groups.google.com/group/comp.lang.c++/msg/6d4f7b0a262826b6

Cheers! --M

E. Mark Ping

unread,
Dec 30, 2005, 5:49:11 PM12/30/05
to
In article <1135965541....@g14g2000cwa.googlegroups.com>,

mlimber <mli...@gmail.com> wrote:
>It may well have been just an OS dependency, not a non-conformancy in
>the compiler. See Jack Klein's post:
>
>http://groups.google.com/group/comp.lang.c++/msg/6d4f7b0a262826b6

Ah, that would explain it. Thanks for setting me straight. It was a
port from Linux to Win32.
--
Mark Ping
ema...@soda.CSUA.Berkeley.EDU

Pete Becker

unread,
Jan 2, 2006, 6:13:44 AM1/2/06
to
E. Mark Ping wrote:
> In article <r8mdndAn6IR...@giganews.com>,
> Pete Becker <peteb...@acm.org> wrote:
>
>>
>>When passed to fopen, "w" says to open for writing in text mode. "wb"
>>says to open for writing in binary mode. VC and GCC both implement that
>>correctly.
>
>
> That may be the case now, but for GCC it wasn't on the version I was
> porting from in 2002.

If so, the library had a serious bug. That's been the rule since time
immemorial. Or, at least, since the original C standard.

E. Mark Ping

unread,
Jan 2, 2006, 1:26:25 PM1/2/06
to
In article <Xu6dnX6JG_R...@giganews.com>,
Pete Becker <peteb...@acm.org> wrote:

>E. Mark Ping wrote:
>> That may be the case now, but for GCC it wasn't on the version I was
>> porting from in 2002.
>
>If so, the library had a serious bug. That's been the rule since time
>immemorial. Or, at least, since the original C standard.

I think Jack Klein corrected me in that on the Linux OS binary is the
same as text mode.
--
Mark Ping
ema...@soda.CSUA.Berkeley.EDU

Pete Becker

unread,
Jan 2, 2006, 1:29:16 PM1/2/06
to
E. Mark Ping wrote:
>
> I think Jack Klein corrected me in that on the Linux OS binary is the
> same as text mode.

Yes, some systems hide the difference. <g>

Roberto Waltman

unread,
Jan 2, 2006, 1:56:45 PM1/2/06
to
<mjba...@yahoo.com> wrote:
>Thanks. #pragmas didn't work, but someone pointed out some file
>specific settings that did.

Being curious: If your problem was indeed related to
opening files in text vs. binary mode, why changing
the optimization settings caused the problem to go
away?

0 new messages