473,657 Members | 2,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(arg v[1]);
QPC(end);
printf("operati on 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 1770
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(arg v[1]); dummyvar += runtimefunc(arg v[1]);

This would ensure that the function will be called each time through the
loop. QPC(end);
printf("operati on 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*******@aust in.rr.com> skrev i meddelandet
news:32******** *****@individua l.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(arg v[1]);

dummyvar += runtimefunc(arg v[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*********@su n.com> skrev i meddelandet
news:cq******** **@news1brm.Cen tral.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******** ******@TK2MSFTN GP12.phx.gbl...

"Artie Gold" <ar*******@aust in.rr.com> skrev i meddelandet
news:32******** *****@individua l.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(arg v[1]);

dummyvar += runtimefunc(arg v[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.ne t> "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(arg v[1]);
QPC(end);
printf("operati on 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.ne t> "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

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

Similar topics

15
6504
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 login info for every call. Is there a way to do this with the xml-rpc code in the standard library? Thanks in advance
9
2392
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'; $sentence = "$insert or not $insert. That is the question."; or
2
5127
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, period_id INT ) BEGIN ATOMIC SET CURRENT QUERY OPTIMIZATION 1;
12
6175
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 \ db2 (cont.) => enable query optimization) DB20000I The SQL command completed successfully. db2 => insert into emp values(1,'m')
11
369
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* after the first loop. <psuedocode> #ifdef USE_MY_FUNC #define testfunc(x) myfunc(x) #else #define testfunc(x) runtimefunc(x) #endif QPC(start);
24
8603
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 have used. Original code : c= 0 ; for (i=0; i<999; i++)
21
2564
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 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...
5
2384
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
2339
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
8310
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8826
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7330
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6166
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.