473,326 Members | 2,125 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,326 software developers and data experts.

Missed jmp Optimization?

I am using MSVC++ 2005, and compiling with /02, Maximum Speed (/0x Full
Optimizations doesn't change anything).

Never mind what my code does, it's what the compiler does with my code that
I'm posting about.

This will generate a forward jump, basically skipping over a move (increment)
if (BinaryHeap[current2].key > BinaryHeap[current2+1].key) {
++current2;
}

This code here is much faster:
current2 += (BinaryHeap[current2].key > BinaryHeap[current2+1].key);
Disassembler shows it generates an xor, setg, and add, but no jmp.

Assuming the branches are roughly 50/50, which in my case they are, the code
without the jump is much faster. In my benchmark, the cycles dropped from
about 14k to 12k, because the jump is inside a tight loop.
I don't know what the ratio of branch taken vs. branch not taken would have
to be for the default code to be faster. I can only guess that this
optimization wasn't used because it wasn't a sure optimization. But IMO it's
pretty close to a sure thing. I'm sure there is a way for a programmer who
knows a jump will be taken less often to write his code in such a way that it
generates a single jump rather than multiple ops, and is therefore faster.
So I think the code without the jump should be generated by default by the
compiler. This allows me to have my cake (readability) and eat it too
(performance).

Thoughts? Code available upon request.
May 10 '06 #1
1 937
AalaarDB wrote:
I am using MSVC++ 2005, and compiling with /02, Maximum Speed (/0x
Full Optimizations doesn't change anything).

Never mind what my code does, it's what the compiler does with my
code that I'm posting about.

This will generate a forward jump, basically skipping over a move
(increment) if (BinaryHeap[current2].key >
BinaryHeap[current2+1].key) { ++current2;
}

This code here is much faster:
current2 += (BinaryHeap[current2].key > BinaryHeap[current2+1].key);
Disassembler shows it generates an xor, setg, and add, but no jmp.

Assuming the branches are roughly 50/50, which in my case they are,
the code without the jump is much faster. In my benchmark, the
cycles dropped from about 14k to 12k, because the jump is inside a
tight loop.
I don't know what the ratio of branch taken vs. branch not taken
would have to be for the default code to be faster. I can only guess
that this optimization wasn't used because it wasn't a sure
optimization. But IMO it's pretty close to a sure thing. I'm sure
there is a way for a programmer who knows a jump will be taken less
often to write his code in such a way that it generates a single jump
rather than multiple ops, and is therefore faster. So I think the
code without the jump should be generated by default by the compiler.
This allows me to have my cake (readability) and eat it too
(performance).

Thoughts? Code available upon request.


Interesting. My thought is that it's too specialized an optimization to be
worth the trouble of modifying the compiler. It's likely that on a typical
code base it'll make an immeausrably small performance improvement. It
sounds like your case is special.

-cd
May 10 '06 #2

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

Similar topics

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...
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 {
2
by: db2admin | last post by:
hi, I have query which runs great when optimization level is changed to 3 but does not run fine with default optimization level of 5. since this is a query in java code, i do not know how can i...
0
by: PaulR | last post by:
Hi, (ignoring data integrity benefits) we would like to understand the extent of the optimization benefits missed for selects when using DISABLE QUERY OPTIMIZATION on NOT ENFORCED FK...
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:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.