473,396 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Over optimization

When performance benchmark testing one of my own functions against the
equivalent runtime function, I found that with /Ox on it optimized away its
own function so that it didn't call it *at all* after the first loop.
<psuedocode>
#ifdef USE_MY_FUNC
#define testfunc(x) myfunc(x)
#else
#define testfunc(x) runtimefunc(x)
#endif
QPC(start);
for(long i = 1; i<BIGNUMBER; i++)
dummyvar = testfunc(argv[1]);
QPC(mid);
for(long i = 1; i<BIGNUMBER; i++)
dummyvar = runtimefunc(argv[1]);
QPC(end);
printf("operation took %I64u\n", mid - start);
printf("control took %I64u\n", end - mid);
</code>

I think what is happening is that with /Ox on, it's completely done away
with the first (BIGNUMBER - 1) loops, knowing that the only thing that
changes is 'dummy', etc....
my dilemma is that:
I don't want to force it to "use" the value on all the loops, say by
printing it to the screen, as this would heavily dilute the time of my
function, i.e. most of the time of each loop wouldn't be spent doing the
function being tested, it would be spent printing.
I don't want to put debug compilation arguments on as this would make it
completely dumb and possibly create the illusion that my own function is
better than a standard implementation when perhaps it isn't when my program
is compiled in release mode.

So, my question is what compiler settings is the best to put on so that the
compiler isn't completely dumb in terms of optimization, but isn't a
absolute complete smartass either. (i.e. as close as possible to release
mode, to give a fair test, but to knock off the one (or more) settings that
allow it to completely eliminate calls)
i.e. make the calls that *I'm* telling it to do, but to do them as fast as
possible. i.e. i'm telling it what I want it to actually *do*, not what I
want the end result to be.

Hope this makes sense

Thanks

Jul 22 '05 #1
11 1737
Bonj wrote:
When performance benchmark testing one of my own functions against the
equivalent runtime function, I found that with /Ox on it optimized away its
own function so that it didn't call it *at all* after the first loop.
Please be advised that you're *off topic* for both c.l.c and c.l.c++ (as
it has to do with an implementation rather tan either language itself).
Find and read the appropriate FAQs before posting in either group again
(it's the Usenet way).

That said, why not the following:
<psuedocode>
#ifdef USE_MY_FUNC
#define testfunc(x) myfunc(x)
#else
#define testfunc(x) runtimefunc(x)
#endif
QPC(start);
for(long i = 1; i<BIGNUMBER; i++)
dummyvar = testfunc(argv[1]); dummyvar += testfunc(argv[1]); QPC(mid);
for(long i = 1; i<BIGNUMBER; i++)
dummyvar = runtimefunc(argv[1]); dummyvar += runtimefunc(argv[1]);

This would ensure that the function will be called each time through the
loop. QPC(end);
printf("operation took %I64u\n", mid - start);
printf("control took %I64u\n", end - mid);
</code>

I think what is happening is that with /Ox on, it's completely done away
with the first (BIGNUMBER - 1) loops, knowing that the only thing that
changes is 'dummy', etc....
my dilemma is that:
I don't want to force it to "use" the value on all the loops, say by
printing it to the screen, as this would heavily dilute the time of my
function, i.e. most of the time of each loop wouldn't be spent doing the
function being tested, it would be spent printing.
I don't want to put debug compilation arguments on as this would make it
completely dumb and possibly create the illusion that my own function is
better than a standard implementation when perhaps it isn't when my program
is compiled in release mode.

So, my question is what compiler settings is the best to put on so that the
compiler isn't completely dumb in terms of optimization, but isn't a
absolute complete smartass either. (i.e. as close as possible to release
mode, to give a fair test, but to knock off the one (or more) settings that
allow it to completely eliminate calls)
i.e. make the calls that *I'm* telling it to do, but to do them as fast as
possible. i.e. i'm telling it what I want it to actually *do*, not what I
want the end result to be.


HTH,
--ag
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Jul 22 '05 #2
Bonj wrote:
When performance benchmark testing one of my own functions against the
equivalent runtime function, I found that with /Ox on it optimized away its
own function so that it didn't call it *at all* after the first loop.
[...]


First, please see Artie Gold's comments on topicality
and his suggestion on how to reduce the optimization.

Second, another method that often defeats aggressive
optimizers is to compile your function separately from the
timing harness. When the compiler processes the timing
program it does not "see" your function and thus can't
detect that it has no side effects, can't (usually) in-line
it, and so on.

Third, still another technique is to have the timing
program use a function pointer to call the functions being
tested, and to set the pointer's value based on information
not available at compile time. For example,

extern void func1(void);
extern void func2(void);
int main(int argc, char **argv) {
void (*func)(void) = (argc == 1) ? func1 : func2;
/* now run your loop, calling `func' each time */

Finally, it is often a good idea to run the tested
function at least once before you start the timing loop.
That helps to ensure that its code pages and whatever data
pages it references are memory-resident before you begin;
any paging or address-mapping or MMU adjustments or other
miscellany that occur only on the very first call will be
less likely to pollute your timing results.

Obtaining a timing that is accurate and unbiased *and*
useful can be a surprisingly tricky business.

--
Er*********@sun.com

Jul 22 '05 #3

"Artie Gold" <ar*******@austin.rr.com> skrev i meddelandet
news:32*************@individual.net...
Bonj wrote:
When performance benchmark testing one of my own functions against
the equivalent runtime function, I found that with /Ox on it
optimized away its own function so that it didn't call it *at all*
after the first loop.


Please be advised that you're *off topic* for both c.l.c and c.l.c++
(as it has to do with an implementation rather tan either language
itself). Find and read the appropriate FAQs before posting in either
group again (it's the Usenet way).

That said, why not the following:
<psuedocode>
#ifdef USE_MY_FUNC
#define testfunc(x) myfunc(x)
#else
#define testfunc(x) runtimefunc(x)
#endif
QPC(start);
for(long i = 1; i<BIGNUMBER; i++)
dummyvar = testfunc(argv[1]);

dummyvar += testfunc(argv[1]);
QPC(mid);
for(long i = 1; i<BIGNUMBER; i++)
dummyvar = runtimefunc(argv[1]);

dummyvar += runtimefunc(argv[1]);

This would ensure that the function will be called each time through
the loop.


No, you can't be sure.

If the compiler is smart enough to realize that runtimefunc always
returns the same result, it might also be smart enough to multiply that
result by BIGNUMBER.
Bo Persson
Jul 22 '05 #4

"Eric Sosman" <er*********@sun.com> skrev i meddelandet
news:cq**********@news1brm.Central.Sun.COM...
Bonj wrote:
When performance benchmark testing one of my own functions against
the
equivalent runtime function, I found that with /Ox on it optimized
away its
own function so that it didn't call it *at all* after the first loop.
[...]
First, please see Artie Gold's comments on topicality
and his suggestion on how to reduce the optimization.

Second, another method that often defeats aggressive
optimizers is to compile your function separately from the
timing harness. When the compiler processes the timing
program it does not "see" your function and thus can't
detect that it has no side effects, can't (usually) in-line
it, and so on.


That doesn't work well for compilers with global or link-time
optimizers. MSVC7.x comes to mind...

Third, still another technique is to have the timing
program use a function pointer to call the functions being
tested, and to set the pointer's value based on information
not available at compile time. For example,

extern void func1(void);
extern void func2(void);
int main(int argc, char **argv) {
void (*func)(void) = (argc == 1) ? func1 : func2;
/* now run your loop, calling `func' each time */
But now you are testing how fast the functions are running when they are
not "properly" optimized. That greatly reduces the value of the test.


Finally, it is often a good idea to run the tested
function at least once before you start the timing loop.
That helps to ensure that its code pages and whatever data
pages it references are memory-resident before you begin;
any paging or address-mapping or MMU adjustments or other
miscellany that occur only on the very first call will be
less likely to pollute your timing results.
This also indicates that the functions might behave differently when run
in a real program. That is where you have to test them eventually.


Obtaining a timing that is accurate and unbiased *and*
useful can be a surprisingly tricky business.


Indeed!
Bo Persson
Jul 22 '05 #5
Exactly. Only printing or writing to a file will do, and they're both too
slow, thus will outweigh the algorithm 99/1 style, thus dilute the figures.

"Bo Persson" <bo*@gmb.dk> wrote in message
news:OE**************@TK2MSFTNGP12.phx.gbl...

"Artie Gold" <ar*******@austin.rr.com> skrev i meddelandet
news:32*************@individual.net...
Bonj wrote:
When performance benchmark testing one of my own functions against the
equivalent runtime function, I found that with /Ox on it optimized away
its own function so that it didn't call it *at all* after the first
loop.


Please be advised that you're *off topic* for both c.l.c and c.l.c++ (as
it has to do with an implementation rather tan either language itself).
Find and read the appropriate FAQs before posting in either group again
(it's the Usenet way).

That said, why not the following:
<psuedocode>
#ifdef USE_MY_FUNC
#define testfunc(x) myfunc(x)
#else
#define testfunc(x) runtimefunc(x)
#endif
QPC(start);
for(long i = 1; i<BIGNUMBER; i++)
dummyvar = testfunc(argv[1]);

dummyvar += testfunc(argv[1]);
QPC(mid);
for(long i = 1; i<BIGNUMBER; i++)
dummyvar = runtimefunc(argv[1]);

dummyvar += runtimefunc(argv[1]);

This would ensure that the function will be called each time through the
loop.


No, you can't be sure.

If the compiler is smart enough to realize that runtimefunc always returns
the same result, it might also be smart enough to multiply that result by
BIGNUMBER.
Bo Persson

Jul 22 '05 #6
Finally, it is often a good idea to run the tested
function at least once before you start the timing loop.
That helps to ensure that its code pages and whatever data
pages it references are memory-resident before you begin;
any paging or address-mapping or MMU adjustments or other
miscellany that occur only on the very first call will be
less likely to pollute your timing results.


That's the purpose of the second operation - the loop from QPC(mid) to
QPC(end) is the control, that should remain approximately the same.
Jul 22 '05 #7
In article <32*************@individual.net> "Bonj" <a@b.com> writes:
That's the purpose of the second operation - the loop from QPC(mid) to
QPC(end) is the control, that should remain approximately the same.


What I do not understand is why you optimise the loop itself. In
general the calling sequence can not be improved very much. You
should just compile your function fully optimised, and the calling
loops unoptimised.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Jul 22 '05 #8


Bonj wrote:
When performance benchmark testing one of my own functions against the
equivalent runtime function, I found that with /Ox on it optimized away its
own function so that it didn't call it *at all* after the first loop.
<psuedocode>
#ifdef USE_MY_FUNC
#define testfunc(x) myfunc(x)
#else
#define testfunc(x) runtimefunc(x)
#endif
QPC(start);
for(long i = 1; i<BIGNUMBER; i++)
dummyvar = testfunc(argv[1]);
QPC(mid);
for(long i = 1; i<BIGNUMBER; i++)
dummyvar = runtimefunc(argv[1]);
QPC(end);
printf("operation took %I64u\n", mid - start);
printf("control took %I64u\n", end - mid);
</code>

I think what is happening is that with /Ox on, it's completely done away
with the first (BIGNUMBER - 1) loops, knowing that the only thing that
changes is 'dummy', etc....
my dilemma is that:
I don't want to force it to "use" the value on all the loops, say by
printing it to the screen, as this would heavily dilute the time of my
function, i.e. most of the time of each loop wouldn't be spent doing the
function being tested, it would be spent printing.
I don't want to put debug compilation arguments on as this would make it
completely dumb and possibly create the illusion that my own function is
better than a standard implementation when perhaps it isn't when my program
is compiled in release mode.

So, my question is what compiler settings is the best to put on so that the
compiler isn't completely dumb in terms of optimization, but isn't a
absolute complete smartass either. (i.e. as close as possible to release
mode, to give a fair test, but to knock off the one (or more) settings that
allow it to completely eliminate calls)
i.e. make the calls that *I'm* telling it to do, but to do them as fast as
possible. i.e. i'm telling it what I want it to actually *do*, not what I
want the end result to be.

Hope this makes sense

Thanks


You can always make 'dummyvar' volatile. Then the compiler cannot remove
any access to it.

//Peter
Jul 22 '05 #9
Well I do optimize the function itself.
But I don't want to un-optimize the loop entirely because that would be
unfair on the runtime function, which is called directly from within the
loop.
Although I suppose I could put it in a separate compilation unit and just
expose it through delegatio - the only reason for me not wanting to do that,
is that it would be slightly different from the way it would be compiled in
real life, but probably not much different speedwise.
"Dik T. Winter" <Di********@cwi.nl> wrote in message
news:I9********@cwi.nl...
In article <32*************@individual.net> "Bonj" <a@b.com> writes:
That's the purpose of the second operation - the loop from QPC(mid) to
QPC(end) is the control, that should remain approximately the same.


What I do not understand is why you optimise the loop itself. In
general the calling sequence can not be improved very much. You
should just compile your function fully optimised, and the calling
loops unoptimised.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland,
+31205924131
home: bovenover 215, 1025 jn amsterdam, nederland;
http://www.cwi.nl/~dik/

Jul 22 '05 #10
Try copying argv[1] to a volatile char * variable and use that instead
of argv[1] directly. Then the compiler will not be able to optimize
out the call (but it will also force it to perform a redundant reload
of the address -- so depending on the runtime function you are testing,
this can artificially skew your results.
--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Jul 22 '05 #11
In article <32*************@individual.net> "Bonj" <a@b.com> writes:
Well I do optimize the function itself.
But I don't want to un-optimize the loop entirely because that would be
unfair on the runtime function, which is called directly from within the
loop.
Although I suppose I could put it in a separate compilation unit and just
expose it through delegatio - the only reason for me not wanting to do that,
is that it would be slightly different from the way it would be compiled in
real life, but probably not much different speedwise.


On most systems it will not matter whether the function is given in another
unit or in the same unit. Optimising timing loops nearly always leads to
misleading results, especially with the current aggressive optimisers, that
is why those should be compiled as written. What you could do to eliminate
the time for the loop itself (including calls to the function) is add a
loop with a call to an empty function. You can subtract that from the
timings for your real functions, and then you will have a fair comparison.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Jul 22 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: The Fumigator | last post by:
Hi. I want to be able to create a persistent connection between an XML-RPC client and server... i.e. I want to be able to login once, carry out a number of calls and then logout rather than send...
9
by: Rune | last post by:
Is it best to use double quotes and let PHP expand variables inside strings, or is it faster to do the string manipulation yourself manually? Which is quicker? 1) $insert = 'To Be';...
2
by: Eugene | last post by:
I am trying to set query optimization class in a simple SQL UDF like this: CREATE FUNCTION udftest ( in_item_id INT ) SPECIFIC udftest MODIFIES SQL DATA RETURNS TABLE( location_id INT,...
12
by: WantedToBeDBA | last post by:
Hi all, db2 => create table emp(empno int not null primary key, \ db2 (cont.) => sex char(1) not null constraint s_check check \ db2 (cont.) => (sex in ('m','f')) \ db2 (cont.) => not enforced...
11
by: Bonj | last post by:
When performance benchmark testing one of my own functions against the equivalent runtime function, I found that with /Ox on it optimized away its own function so that it didn't call it *at all*...
24
by: Kunal | last post by:
Hello, I need help in removing if ..else conditions inside for loops. I have used the following method but I am not sure whether it has actually helped. Below is an example to illustrate what I...
21
by: mjbackues at yahoo | last post by:
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...
5
by: wkaras | last post by:
I've compiled this code: const int x0 = 10; const int x1 = 20; const int x2 = 30; int x = { x2, x0, x1 }; struct Y {
20
by: Ravikiran | last post by:
Hi Friends, I wanted know about whatt is ment by zero optimization and sign optimization and its differences.... Thank you...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.