473,395 Members | 1,726 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,395 software developers and data experts.

C#/.net efficiency

Two simple questions:
1) Shouldn't the code snippets below produce the same IL?
2) Which will be faster?

TIA!

// Bit shift method
return (19 * (y >> 4)) + (x >> 4) + 1;

IL:
{
// Code size 13 (0xd)
.maxstack 8
IL_0000: ldc.i4.s 19
IL_0002: ldarg.2
IL_0003: ldc.i4.4
IL_0004: shr
IL_0005: mul
IL_0006: ldarg.1
IL_0007: ldc.i4.4
IL_0008: shr
IL_0009: add
IL_000a: ldc.i4.1
IL_000b: add
IL_000c: ret
}

// Mathematical equivalent
return (19 * (y /16) + (x / 16) + 1);

IL:
{
// Code size 20 (0x14)
.maxstack 3
.locals init ([0] int32 CS$1$0000)
IL_0000: nop
IL_0001: ldc.i4.s 19
IL_0003: ldarg.2
IL_0004: ldc.i4.s 16
IL_0006: div
IL_0007: mul
IL_0008: ldarg.1
IL_0009: ldc.i4.s 16
IL_000b: div
IL_000c: add
IL_000d: ldc.i4.1
IL_000e: add
IL_000f: stloc.0
IL_0010: br.s IL_0012
IL_0012: ldloc.0
IL_0013: ret
}
Jul 21 '05 #1
3 1357
msnews.microsoft.com <st********@yahoo.com> wrote:
Two simple questions:
1) Shouldn't the code snippets below produce the same IL?
No - they might give different results if x or y are negative, for one
thing.
2) Which will be faster?


I'd expect the shift to be faster, but the obvious course of action is
to try it. However, unless this code is in the bottleneck of your app,
the chances are very slim that it'll make any significant difference.
Write it in the form you find most readable.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
Thanks for the response. I hadn't considered negative values for x/y only
because that should never be the case in my app. It does makes sense though
when you consider that they could be negative. As far as speed goes, I also
expect the bit shift to be a tiny bit faster...but as you said, it shouldn't
really be enough to matter.

Thanks!

now I'm off to figure out where to change my usename in OE :)


"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
msnews.microsoft.com <st********@yahoo.com> wrote:
Two simple questions:
1) Shouldn't the code snippets below produce the same IL?


No - they might give different results if x or y are negative, for one
thing.
2) Which will be faster?


I'd expect the shift to be faster, but the obvious course of action is
to try it. However, unless this code is in the bottleneck of your app,
the chances are very slim that it'll make any significant difference.
Write it in the form you find most readable.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #3
msnews.microsoft.com wrote:
Thanks for the response. I hadn't considered negative values for x/y only
because that should never be the case in my app. It does makes sense though
when you consider that they could be negative. As far as speed goes, I also
expect the bit shift to be a tiny bit faster...but as you said, it shouldn't
really be enough to matter.

Thanks!

now I'm off to figure out where to change my usename in OE :)


"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
msnews.microsoft.com <st********@yahoo.com> wrote:
Two simple questions:
1) Shouldn't the code snippets below produce the same IL?


No - they might give different results if x or y are negative, for one
thing.

2) Which will be faster?


I'd expect the shift to be faster, but the obvious course of action is
to try it. However, unless this code is in the bottleneck of your app,
the chances are very slim that it'll make any significant difference.
Write it in the form you find most readable.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



I'd just like to add - the difference is not in the division itself, but
in the compensation for the shift of negative numbers Jon mentioned.

This is the output of the JIT:

Shift version: Divide version:

00000000 push ebp 00000000 push ebp
00000001 mov ebp,esp 00000001 mov ebp,esp
00000003 sub esp,8 00000003 sub esp,8
00000006 push edi 00000006 push edi
00000007 push esi 00000007 push esi
00000008 mov edi,ecx 00000008 mov edi,ecx
0000000a mov esi,edx 0000000a mov esi,edx
0000000c mov eax,esi 0000000c mov ecx,esi
0000000e test ecx,ecx
00000010 jns 00000015
00000012 add ecx,0Fh
0000000e sar eax,4 00000015 sar ecx,4
00000011 imul eax,eax,13h 00000018 imul ecx,ecx,13h
00000014 mov edx,edi 0000001b mov eax,edi
0000001d test eax,eax
0000001f jns 00000024
00000021 add eax,0Fh
00000016 sar edx,4 00000024 sar eax,4
00000019 lea eax,[eax+edx+1] 00000027 lea eax,[ecx+eax+1]
0000001d pop esi 0000002b pop esi
0000001e pop edi 0000002c pop edi
0000001f mov esp,ebp 0000002d mov esp,ebp
00000021 pop ebp 0000002f pop ebp
00000022 ret 00000030 ret
Once you change the parameters to uint, the compiled funcitons become
almost equal, except for register usage, which should not affect
performance on todays processors ;)

HTH,
Stefan
Jul 21 '05 #4

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

Similar topics

2
by: Sara | last post by:
Hi - I've been reading the posts for a solution to my query, and realize that I should ask an "approch" question as well. We receive our production data from a third party, so my uers import...
31
by: mark | last post by:
Hello- i am trying to make the function addbitwise more efficient. the code below takes an array of binary numbers (of size 5) and performs bitwise addition. it looks ugly and it is not elegant...
92
by: Dave Rudolf | last post by:
Hi all, Normally, I would trust that the ANSI libraries are written to be as efficient as possible, but I have an application in which the majority of the run time is calling the acos(...)...
9
by: Peng Jian | last post by:
I have a function that is called very very often. Can I improve its efficiency by declaring its local variables to be static?
9
by: travisperkins03 | last post by:
Hi, I have read somewhere that C code sometimes cannot be compiled to be as efficient as FORTRAN, eg for matrix multiplication, because a C compiler cannot make the assumptions about arrays that...
1
by: Tomás | last post by:
dynamic_cast can be used to obtain a pointer or to obtain a reference. If the pointer form fails, then you're left with a null pointer. If the reference form fails, then an exception is thrown....
335
by: extrudedaluminiu | last post by:
Hi, Is there any group in the manner of the C++ Boost group that works on the evolution of the C language? Or is there any group that performs an equivalent function? Thanks, -vs
19
by: vamshi | last post by:
Hi all, This is a question about the efficiency of the code. a :- int i; for( i = 0; i < 20; i++ ) printf("%d",i); b:- int i = 10;
9
by: OldBirdman | last post by:
Efficiency I've never stumbled on any discussion of efficiency of various methods of coding, although I have found posts on various forums where individuals were concerned with efficiency. I'm...
5
by: want.to.be.professer | last post by:
For OO design, I like using virtual member function.But considering efficiency, template is better. Look at this program, class Animal { public: virtual void Walk() = 0; }; class Dog
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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
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...

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.