Discussion:
Microsoft Visual C++ 6.0 compiler vs Borland C++ Builder 5 compiler speed optimization.
(too old to reply)
Alex
2005-06-27 16:19:44 UTC
Permalink
I know that this post should be in the Borland site (and it is) but I am
getting no luck there so I thought I'd ask here (don't laugh). We use both
Microsoft and Borland here (mostly MS but one project uses Borland C++
Builder 5). We have had many complaints from customers that parts of our
program run way too slow (our math calculations). So I made the following
test program:

double x, y, z, tot = 0.0;
for (int j = 0; j < 1000; ++j)
{
for (int k = 0; k < 1000000; ++k)
{
x = 2;
y = .5;
z = x * x / (y + 2);
tot += z;
}
}

Yes I know the code is totally inefficient. It IS suppose to be. I want the
compiler to work for me in this case. When I compile this in Borland debug
114 sec., in MS debug about 114, in Borland release (compiler
flags -O2, -O, -Ov, -OS, -ff) still about 114, MS 5 sec.. I couldn't believe
it. The problem is that the project IS in Borland, it has hundreds of
thousands of lines of calculations (in many many loops) and I'm stuck. It is
as if the loop optimization flag does not work (with no caculations, just
the loops and a counter, the time difference is still overwelming). I am
using the math coprocessor, I check. Have you heard of such time
differences? If so, have you heard how to improve such difference using the
Borland compiler? I feel that I must not be setting something correctly in
Borland but I don't know what. I have posted this in Borland newgroups too
but people just keep responding that the code is inefficient and I should
rewrite it in ASM. They are missing the point!!!! Any help would be greatly
appreciated. Thanks in advance. Alex
Duane Hebert
2005-06-27 19:23:04 UTC
Permalink
"Alex" <***@aqaaa.jjjj> wrote in message news:***@TK2MSFTNGP15.phx.gbl...
<snip>
Post by Alex
using the math coprocessor, I check. Have you heard of such time
differences? If so, have you heard how to improve such difference using the
Borland compiler? I feel that I must not be setting something correctly in
Borland but I don't know what. I have posted this in Borland newgroups too
but people just keep responding that the code is inefficient and I should
rewrite it in ASM. They are missing the point!!!! Any help would be greatly
appreciated. Thanks in advance. Alex
From what I can see advice was to
profile this and see where the actual bottleneck was and post that.
It was also suggested that you replace math.h with fastmath.h.

Having used both BCB6 and MSVC7.1, I would say that MSVC seems
quicker but I've never profiled it.

I wouldn't expect much in the way of support though as both BCB5
and MSVC6 are not current products.
Walter Briscoe
2005-06-27 16:56:39 UTC
Permalink
Post by Alex
I know that this post should be in the Borland site (and it is) but I am
Why did you not cross-post?
Post by Alex
getting no luck there so I thought I'd ask here (don't laugh). We use both
Microsoft and Borland here (mostly MS but one project uses Borland C++
Builder 5). We have had many complaints from customers that parts of our
program run way too slow (our math calculations). So I made the following
double x, y, z, tot = 0.0;
for (int j = 0; j < 1000; ++j)
{
for (int k = 0; k < 1000000; ++k)
{
x = 2;
y = .5;
z = x * x / (y + 2);
tot += z;
}
}
This is a bad benchmark because it can easily be optimised.
x = 2;
y = .5;
z = x * x / ( y + 2 ); // z = 0.4
tot += z; tot += 0.4;

for ( int k ... ) ... is equivalent to tot += 4E6;
The total thing is equivalent to tot = 4E9;
If you want to set up a benchmark, use functions - probably in different
compilation units.

If you have speed problems, profile the code and focus on the hot spots.
--
Walter Briscoe
David Cameron
2005-06-28 09:55:49 UTC
Permalink
Post by Alex
I know that this post should be in the Borland site (and it is) but I am
getting no luck there so I thought I'd ask here (don't laugh). We use both
Microsoft and Borland here (mostly MS but one project uses Borland C++
Builder 5). We have had many complaints from customers that parts of our
program run way too slow (our math calculations). So I made the following
double x, y, z, tot = 0.0;
for (int j = 0; j < 1000; ++j)
{
for (int k = 0; k < 1000000; ++k)
{
x = 2;
y = .5;
z = x * x / (y + 2);
tot += z;
}
}
Yes I know the code is totally inefficient. It IS suppose to be. I want the
compiler to work for me in this case. When I compile this in Borland debug
114 sec., in MS debug about 114, in Borland release (compiler
flags -O2, -O, -Ov, -OS, -ff) still about 114, MS 5 sec.. I couldn't believe
it. The problem is that the project IS in Borland, it has hundreds of
thousands of lines of calculations (in many many loops) and I'm stuck. It is
as if the loop optimization flag does not work (with no caculations, just
the loops and a counter, the time difference is still overwelming). I am
using the math coprocessor, I check. Have you heard of such time
differences? If so, have you heard how to improve such difference using the
Borland compiler? I feel that I must not be setting something correctly in
Borland but I don't know what. I have posted this in Borland newgroups too
but people just keep responding that the code is inefficient and I should
rewrite it in ASM. They are missing the point!!!! Any help would be greatly
appreciated. Thanks in advance. Alex
Hi Alex,
Like Walter says you need to put your code in functions and use less
constant expressions.
Also return a value from your functions, make calls to other functions etc

I tried wrapping your code in a funtion with no return value i.e.

void test()
{
double x, y, z, tot = 0.0;
for (int j = 0; j < 1000; ++j)
{
for (int k = 0; k < 1000000; ++k)
{
x = 2;
y = .5;
z = x * x / (y + 2);
tot += z;
}
}
}

VS2003 optemises this function (in release mode) to a single asm line:

ret 0

This is because the function doesn't return a value or alter any external
variables, or call any functions.

HTH,
Dave
Ali
2005-06-28 10:59:42 UTC
Permalink
Humm.. really interesting discussion here. So Alex Borland people are
not responding you with reasonable question. Its stange to see how big
difference is;-)
Post by Alex
people just keep responding that the code is inefficient and I should
rewrite it in ASM

Nope thats not gonna work, though it will improve some performance
slightly.

Addition to Walter Briscoe post:
This is also same solution as above while living in C++ world.

Try to write inline function with this minimum code in C++,, i'm sure
this is also not the solution , To me it is some thing with Borlan
compiler.

double x, y, z, tot = 0.0;
for (int j = 0; j < 1000; ++j , )
for (int k = 0; k < 1000000; ++k , x = 2 , y = .5 , z = x * x / (y
+ 2), tot += z);

Have tried it wrapping this code in Macro?
Darko Miletic
2005-07-04 12:42:32 UTC
Permalink
Post by Alex
I feel that I must not be setting something correctly in
Borland but I don't know what. I have posted this in Borland newgroups too
but people just keep responding that the code is inefficient and I should
rewrite it in ASM. They are missing the point!!!! Any help would be greatly
appreciated. Thanks in advance. Alex
You are missing the point here. As I told you in the thread at Borland
newsgroups Borland compiler can not optimise this code as good as MSVC.
Period. Accept this as a fact. It is indeed known fact that this
compiler is not the finest option when it comes to math code.

In order to get some speed boost with Borland compiler you have 3 options:

1. Write asm code
2. Move math code to msvc dll and use that from your main code
3. Wait for new CBuilder to see if they fix the math optimisation.

Darko

Loading...