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

Is callvirt faster than call ?

I've made some small examples in VB and C# thay were doing this:
private void Form1_Load(object sender, System.EventArgs e){

int b = 9966;

Doh (b); }

and that

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim b As Int32 = 9966

Doh(b)

End Sub

Public Sub Doh(ByRef a As Integer)

End Sub

i've compiled tham and decompiled using Salamander
(http://www.remotesoft.com/salamander/index.html)

My intention was to see how do pointer work in C# with regards to VB.net
byref.
What i've seen was a big difference that may cause big speed decrease in vb.
I just don't know why because reference are in practice safe way of
transfering variables' addresses.
look

.maxstack 2 //same
.locals (int32) //same

IL_0000: ldc.i4 9966 //same
IL_0005: stloc.0 //same
IL_0006: ldarg.0 //same
IL_0007: ldloca.s 0 //same
IL_0009: call instance void
WindowsApplication5.Form1::Doh(int32*)
IL_000e: ret

vs. (VB)

.maxstack 2 //same
.locals (int32) //same

IL_0000: nop //WHAT FOR!
IL_0001: ldc.i4 9966
IL_0006: stloc.0 //same
IL_0007: ldarg.0 //same
IL_0008: ldloca.s 0 //same
IL_000a: callvirt instance void
WindowsApplication4.Form1::Doh(int32&) // differs with virt and "&"
IL_000f: nop //WHAT FOR!
IL_0010: nop //WHAT FOR!
IL_0011: ret //same

So:
Is callvirt faster than call ?

why can't ref int be called using using call and int32* ? Whas it to
difficult, or is it faster?

What the hell are those NOOPS for? To make VB slower?

I'm shocked!

Can you explain it in any way?


Jul 21 '05 #1
7 1804
I am not shure but, I think that in c# you have to call it Doh(&b);
Excuse me if I am wrong.

Best Regards,
Alejandro Lapeyre

"Doker" <do****@wp.pl> escribió en el mensaje
news:d2**********@node2.news.atman.pl...
I've made some small examples in VB and C# thay were doing this:
private void Form1_Load(object sender, System.EventArgs e){

int b = 9966;

Doh (b); }

and that

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim b As Int32 = 9966

Doh(b)

End Sub

Public Sub Doh(ByRef a As Integer)

End Sub

i've compiled tham and decompiled using Salamander
(http://www.remotesoft.com/salamander/index.html)

My intention was to see how do pointer work in C# with regards to VB.net
byref.
What i've seen was a big difference that may cause big speed decrease in
vb.
I just don't know why because reference are in practice safe way of
transfering variables' addresses.
look

.maxstack 2 //same
.locals (int32) //same

IL_0000: ldc.i4 9966 //same
IL_0005: stloc.0 //same
IL_0006: ldarg.0 //same
IL_0007: ldloca.s 0 //same
IL_0009: call instance void
WindowsApplication5.Form1::Doh(int32*)
IL_000e: ret

vs. (VB)

.maxstack 2 //same
.locals (int32) //same

IL_0000: nop //WHAT FOR!
IL_0001: ldc.i4 9966
IL_0006: stloc.0 //same
IL_0007: ldarg.0 //same
IL_0008: ldloca.s 0 //same
IL_000a: callvirt instance void
WindowsApplication4.Form1::Doh(int32&) // differs with virt and "&"
IL_000f: nop //WHAT FOR!
IL_0010: nop //WHAT FOR!
IL_0011: ret //same

So:
Is callvirt faster than call ?

why can't ref int be called using using call and int32* ? Whas it to
difficult, or is it faster?

What the hell are those NOOPS for? To make VB slower?

I'm shocked!

Can you explain it in any way?


Jul 21 '05 #2
nop.
but thats not the question.
the questions are what is the difference between pointers and ref.
whats is faster, for real (not what C++ programmers say)
why there are so many nops in VB.net code.
Jul 21 '05 #3
Doker wrote:
nop.
but thats not the question.
I can see that you are calling 2 different Form1.Doh's. I don't know
about VB, maybe "Sub"'s are virtual by default? that would explain the
virtual-call in the generated IL.

Virtual vs. non-virtual call-overhead is not related to the types of
arguments at all.

Virtual-calls cannot be cheaper than non-virtual calls but the compiler
and/or .NET-runtime may (JIT-)compile a virtual call to a non-virtual
under certain conditions, or even inline the code from the called function.
the questions are what is the difference between pointers and ref.
whats is faster, for real (not what C++ programmers say)
why there are so many nops in VB.net code.


You do realize that pointers are to memory and ref's are to objects?

Pointers in C# can be obtained using "fixed", which fixes the object in
memory so the garbage-collector cannot move it. That operation is
probably rather expensive, but if you do relative indexing the JIT may
be able to do something faster in loops when it is guaranteed that the
GC cannot move the array around? write a test...

In your specific example, I would guess the C#-compiler has better
optimization, since int is a struct-type and therefore on the stack it
can use a pointer instead of a ref, without fixing it fist.

Try something like this: (uncompiled code ahead :)

class Thing { public Thing Value; }
class A { public virtual f(ref Thing t) { t.Value = t; } }
class B: A { public override f(ref Thing t) { t.Value = null; } }
public class Test {
public static int Main(string[] args) {
Thing t = new Thing();
A a;
// Make sure compiler cannot see which .f is called:
if ( args.Length > 1 )
a = new A();
else
a = new B();
a.f(ref t);
// Make sure t.Value is used
// so compiler cannot optimize a.f(ref t) away
if ( t.Value == null )
return 0;
else
return 1;
}
}
--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Jul 21 '05 #4

"Doker" <do****@wp.pl> wrote in message
news:d2**********@node2.news.atman.pl...
I've made some small examples in VB and C# thay were doing this:
private void Form1_Load(object sender, System.EventArgs e){

int b = 9966;

Doh (b); }

Could you provide the definition of Doh() for the C# code? That IL doesn't
look right at all...
Jul 21 '05 #5
Is callvirt faster than call ?
Most likely not.

why can't ref int be called using using call
It can, but C# often use callvirt for instance method calls even when
call would have worked, because callvirt will throw if called with a
null this reference, which is the behavior specified by the language.

What the hell are those NOOPS for? To make VB slower?


No, to make it debuggable. You should probably look at an optimized
release build instead.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jul 21 '05 #6
> > What the hell are those NOOPS for? To make VB slower?


NOP's are instructions that does nothing. You could regard them as space
filler.

NOP's are normally used to synchronize your next assembler instruction to be
on a memory boundary to speed up executing.
For example, if you have a 32 bits data bus, then adding NOP so that your
next assembler instruction starts at a 4 byte boundary, then it speeds up
loading the instruction in one pass or else the processor must first read
the lower part and then in a next phase read the upper part which would be a
2 pass instruction load, slowing down the processor instruction.

The problem this is IL and I know that IL gets translated to real assembler
by the CLR, so I have no idea if the NOP's will still be there or even if
they have meaning.

--
http://www.skyscan.be

Jul 21 '05 #7
> >What the hell are those NOOPS for? To make VB slower?

No, to make it debuggable. You should probably look at an optimized
release build instead.

That could also be a reason.

A debug built is almost a one to on translation of every function.
A release built is a very optimize assembler instruction set and can change
the order of the instructions that are ordered in such a way that is
executes faster, but have almost no relationship with the original source
code, so very hard to put breakpoints.

For example, a loop is always slower since the processor has to jump back
and thus must flush the already preloaded instructions of the code after the
loop, it loses time to reload the previous code. So a release code could
expand the loop if the loop is small, lets say 2-4 loops max, and thus avoid
the jump back. But ofcourse this means that that there is no debug
information possible to put a breakpoint.

--
http://www.skyscan.be

Jul 21 '05 #8

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

Similar topics

54
by: zhaoyandong | last post by:
In one of my interview, some people asked me why C is faster C++, and tell me to illustrate at least two reasons. I can't find the answer in the web. I'll appreciate any suggestion on this....
65
by: Skybuck Flying | last post by:
Hi, I needed a method to determine if a point was on a line segment in 2D. So I googled for some help and so far I have evaluated two methods. The first method was only a formula, the second...
43
by: Mountain Bikn' Guy | last post by:
I have a situation where an app writes data of various types (primitives and objects) into a single dimensional array of objects. (This array eventually becomes a row in a data table, but that's...
8
by: ThomasR | last post by:
I understand that virtual methods on inherited objects are slower than non-virtual methods because of the indirection required to support the call. However, when looking at IL code produced by...
9
by: VenuGopal | last post by:
Hi, why n++ executes faster than n+1..... or does it realli execute faster? thanks Venugopal.B
7
by: Doker | last post by:
I've made some small examples in VB and C# thay were doing this: private void Form1_Load(object sender, System.EventArgs e){ int b = 9966; Doh (b); } and that Private Sub...
23
by: AndersWang | last post by:
Hi, dose anybody here explain to me why memset would be faster than a simple loop. I doubt about it! In an int array scenario: int array; for(int i=0;i<10;i++) //ten loops
48
by: istillshine | last post by:
When I used gprof to see which function consumed most running time, I identified the following one. sz was less than 5000 on average, but foo had been called about 1,000,000 times. I have tried...
36
by: lak | last post by:
Which is faster? Post Increment or assignment? Why? I was not able to get any things.
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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
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...
0
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...

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.