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

Use of Return Statement

While browsing MSDN, I found an article related to performance in
VB.NET programs. The article is dated 2002, and I am not sure if the
information below is still correct:

http://msdn2.microsoft.com/en-us/lib...perfoptanchor5

Calling and Returning

....

Use the Return statement whenever your logic permits it. For more
information, see Return Statement. The compiler can optimize the code
better than if you use Exit Function, Exit Property, or Exit Sub, or
allow the End Function, End Get, End Set, or End Sub statement to
generate a return.
Now reading this, I get the impressions that I should add a return to
all of my subroutines instead of relying on End Sub. If this is
indeed true, this seems like a compiler bug that I hope has been fixed
by now.

Any comments on this?
Aug 8 '07 #1
8 4066
Jamil,

Remember that all external actions like a .show cost more time than all
other actions from an application. With this you almost in any situation for
sure can not make your program faster for your user.

(There are some application when you can win time, however then the question
comes if the managed code based programs are the best for that. In those
situation can C++ be a good choise). Know that it should be very extreme, we
have seen a real live big sample of an application in this newsgroup where
the VB.Net program had more performance than the same applications in C++.

Cor
<ja***@onepost.netschreef in bericht
news:bq********************************@4ax.com...
While browsing MSDN, I found an article related to performance in
VB.NET programs. The article is dated 2002, and I am not sure if the
information below is still correct:

http://msdn2.microsoft.com/en-us/lib...perfoptanchor5

Calling and Returning

...

Use the Return statement whenever your logic permits it. For more
information, see Return Statement. The compiler can optimize the code
better than if you use Exit Function, Exit Property, or Exit Sub, or
allow the End Function, End Get, End Set, or End Sub statement to
generate a return.
Now reading this, I get the impressions that I should add a return to
all of my subroutines instead of relying on End Sub. If this is
indeed true, this seems like a compiler bug that I hope has been fixed
by now.

Any comments on this?
Aug 8 '07 #2
Cor,

I know what you are stating, but I am not quite sure why you are
stating it. I am not debating whether VB.NET or C/C++ is a better
development language. I simply wanted confirmation on a published
Microsoft recommendation. They spent time and resources on publishing
it, so it must have been for a reason.

Given a VB.NET application that executes as a batch process with code
that executes repeatedly, I would be willing to make code changes that
saves fractions of seconds in a single procedure.

So, based on the link I provided, which actually executes in less
time?

Example 1:
Public Sub Test1(ByRef val As Long)
val += 1
Return
End Sub

Example 2:
Public Sub Test2(ByRef val As Long)
val += 1
End Sub

According to the link, Example 1 is more efficient. My point is that
if this is true, I think it's a defect in the compilation. There
should be absolutely no difference between the two.

Here's a bit more code to put this into perspective:

Public Sub Main()
Dim start1 As Date
Dim finish1 As Date
Dim start2 As Date
Dim finish2 As Date

start1 = Now

For i As Long = 0L To 10000000000L
Call Test1(i)
Next i

finish1 = Now
start2 = Now

For i As Long = 0L To 10000000000L
Call Test2(i)
Next i

finish2 = Now

Call MsgBox(String.Format("Test1: {0} ms Test2: {1} ms", _
finish1.Subtract(start1).TotalMilliseconds, _
finish2.Subtract(start2).TotalMilliseconds))

Executing this test, I received a savings of over one second. There
is a difference in time, and the MSDN article is accurate with Visual
Studio 2005 containing the latest service packs.

On Wed, 8 Aug 2007 19:39:04 +0200, "Cor Ligthert[MVP]"
<no************@planet.nlwrote:
>Jamil,

Remember that all external actions like a .show cost more time than all
other actions from an application. With this you almost in any situation for
sure can not make your program faster for your user.

(There are some application when you can win time, however then the question
comes if the managed code based programs are the best for that. In those
situation can C++ be a good choise). Know that it should be very extreme, we
have seen a real live big sample of an application in this newsgroup where
the VB.Net program had more performance than the same applications in C++.

Cor
Aug 8 '07 #3
What I also find interesting in this is that the generated MSIL code
is identical between the two subroutines:

..method public static void Test1(int64& val) cil managed
{
// Code size 8 (0x8)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldarg.0
IL_0002: ldind.i8
IL_0003: ldc.i4.1
IL_0004: conv.i8
IL_0005: add.ovf
IL_0006: stind.i8
IL_0007: ret
} // end of method Module1::Test1
..method public static void Test2(int64& val) cil managed
{
// Code size 8 (0x8)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldarg.0
IL_0002: ldind.i8
IL_0003: ldc.i4.1
IL_0004: conv.i8
IL_0005: add.ovf
IL_0006: stind.i8
IL_0007: ret
} // end of method Module1::Test2
Aug 8 '07 #4
<ja***@onepost.netschrieb
Given a VB.NET application that executes as a batch process with
code that executes repeatedly, I would be willing to make code
changes that saves fractions of seconds in a single procedure.

So, based on the link I provided, which actually executes in less
time?

Example 1:
Public Sub Test1(ByRef val As Long)
val += 1
Return
End Sub

Example 2:
Public Sub Test2(ByRef val As Long)
val += 1
End Sub

According to the link, Example 1 is more efficient. My point is
that if this is true, I think it's a defect in the compilation.
There
should be absolutely no difference between the two.

Here's a bit more code to put this into perspective:

Public Sub Main()
Dim start1 As Date
Dim finish1 As Date
Dim start2 As Date
Dim finish2 As Date

start1 = Now

For i As Long = 0L To 10000000000L
Call Test1(i)
Next i

finish1 = Now
start2 = Now

For i As Long = 0L To 10000000000L
Call Test2(i)
Next i

finish2 = Now

Call MsgBox(String.Format("Test1: {0} ms Test2: {1} ms", _
finish1.Subtract(start1).TotalMilliseconds,
_
finish2.Subtract(start2).TotalMilliseconds))

Executing this test, I received a savings of over one second. There
is a difference in time, and the MSDN article is accurate with
Visual Studio 2005 containing the latest service packs.

I don't now what execatly the statement that Return can be optimized better
was referrring to, so I can not give a good reason for it. Though, you are
creating an example where Return really doesn't make sense: Just at the end
of a Sub (Sub, not Function).

In your example, it is very simple why Test1 is slower: Return causes a jump
to the end of the procedure. In Test2 this superfluous step is left out,
therefore it's faster.

I pasted the IL code below. You don't have to speak IL, but you see that, in
Test2, there is a "br.s" command at position IL_0010. "br" = branch = jump
(to position IL_0012).

Test1:

IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldarg.0
IL_0003: ldind.i8
IL_0004: ldc.i8 0x1
IL_000d: add.ovf
IL_000e: stind.i8
IL_000f: nop
IL_0010: ret

Tets2:

IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldarg.0
IL_0003: ldind.i8
IL_0004: ldc.i8 0x1
IL_000d: add.ovf
IL_000e: stind.i8
IL_000f: nop
IL_0010: br.s IL_0012
IL_0012: nop
IL_0013: ret
This is the Debug build. Using the Release build, the execution time is
exactly the same, which means that the superfluous jump has been
optimized-away.

Well, it was /you/ putting an additional Return statement there to make it
slower. ;-)
Armin

Aug 8 '07 #5
On Thu, 9 Aug 2007 01:37:53 +0200, "Armin Zingler"
<az*******@freenet.dewrote:

In my test, Test1 is actually faster with a release build. I have a
savings of over one second (which corresponds to the MSDN article).
Is it a hardware difference? Not sure.

Your MSIL is also very different than mine though.
>I don't now what execatly the statement that Return can be optimized better
was referrring to, so I can not give a good reason for it. Though, you are
creating an example where Return really doesn't make sense: Just at the end
of a Sub (Sub, not Function).

In your example, it is very simple why Test1 is slower: Return causes a jump
to the end of the procedure. In Test2 this superfluous step is left out,
therefore it's faster.

I pasted the IL code below. You don't have to speak IL, but you see that, in
Test2, there is a "br.s" command at position IL_0010. "br" = branch = jump
(to position IL_0012).

Test1:

IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldarg.0
IL_0003: ldind.i8
IL_0004: ldc.i8 0x1
IL_000d: add.ovf
IL_000e: stind.i8
IL_000f: nop
IL_0010: ret

Tets2:

IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldarg.0
IL_0003: ldind.i8
IL_0004: ldc.i8 0x1
IL_000d: add.ovf
IL_000e: stind.i8
IL_000f: nop
IL_0010: br.s IL_0012
IL_0012: nop
IL_0013: ret
This is the Debug build. Using the Release build, the execution time is
exactly the same, which means that the superfluous jump has been
optimized-away.

Well, it was /you/ putting an additional Return statement there to make it
slower. ;-)
Armin
Aug 9 '07 #6
I just ran the test a second time to make sure I wasn't imagining
things. These are my results:

With Return: 30062.5 Without Return: 31218.75

Test1 ran in around 30 seconds.
Test2 ran in over 31 seconds.

My build is Release:

Remove integer overflow checks is unchecked
Enable optimizations is checked

Target CPU: AnyCPU

Intel Core 2 Duo 6600
4 gigs RAM
Windows kernel loaded in RAM

On Wed, 08 Aug 2007 20:00:09 -0400, ja***@onepost.net wrote:
>On Thu, 9 Aug 2007 01:37:53 +0200, "Armin Zingler"
<az*******@freenet.dewrote:

In my test, Test1 is actually faster with a release build. I have a
savings of over one second (which corresponds to the MSDN article).
Is it a hardware difference? Not sure.

Your MSIL is also very different than mine though.
Aug 9 '07 #7
Jamil,

It is an old publication based on technical facts. It is discussed more
times and a general answer is to make your program, see where 80 procent of
the time is spent and than optimize this.

Mostly you will see that between 0 and 20% of your program needs the most
time. This will however most probably not be in actions intern in memory
which are done in steps in the cycles which runs in pico seconds however in
the IO including screen handling. The later is often forgot and people makes
easily an extra show or refresh just to be sure it is showed.

Cor

<ja***@onepost.netschreef in bericht
news:6g********************************@4ax.com...
Cor,

I know what you are stating, but I am not quite sure why you are
stating it. I am not debating whether VB.NET or C/C++ is a better
development language. I simply wanted confirmation on a published
Microsoft recommendation. They spent time and resources on publishing
it, so it must have been for a reason.

Given a VB.NET application that executes as a batch process with code
that executes repeatedly, I would be willing to make code changes that
saves fractions of seconds in a single procedure.

So, based on the link I provided, which actually executes in less
time?

Example 1:
Public Sub Test1(ByRef val As Long)
val += 1
Return
End Sub

Example 2:
Public Sub Test2(ByRef val As Long)
val += 1
End Sub

According to the link, Example 1 is more efficient. My point is that
if this is true, I think it's a defect in the compilation. There
should be absolutely no difference between the two.

Here's a bit more code to put this into perspective:

Public Sub Main()
Dim start1 As Date
Dim finish1 As Date
Dim start2 As Date
Dim finish2 As Date

start1 = Now

For i As Long = 0L To 10000000000L
Call Test1(i)
Next i

finish1 = Now
start2 = Now

For i As Long = 0L To 10000000000L
Call Test2(i)
Next i

finish2 = Now

Call MsgBox(String.Format("Test1: {0} ms Test2: {1} ms", _
finish1.Subtract(start1).TotalMilliseconds, _
finish2.Subtract(start2).TotalMilliseconds))

Executing this test, I received a savings of over one second. There
is a difference in time, and the MSDN article is accurate with Visual
Studio 2005 containing the latest service packs.

On Wed, 8 Aug 2007 19:39:04 +0200, "Cor Ligthert[MVP]"
<no************@planet.nlwrote:
>>Jamil,

Remember that all external actions like a .show cost more time than all
other actions from an application. With this you almost in any situation
for
sure can not make your program faster for your user.

(There are some application when you can win time, however then the
question
comes if the managed code based programs are the best for that. In those
situation can C++ be a good choise). Know that it should be very extreme,
we
have seen a real live big sample of an application in this newsgroup where
the VB.Net program had more performance than the same applications in C++.

Cor
Aug 9 '07 #8
Actually, I had totally forgotten one important thing in regards to
this test. Look at the size of the code in the MSIL. During runtime
execution, the compiler is not going to create a sub routine at all.
The code will be executed inline.

I am still curious on the results, so I will correct the experiment.

On Thu, 9 Aug 2007 13:10:01 +0200, "Armin Zingler"
<az*******@freenet.dewrote:
>Yes, the only difference I see is in Debug build. If you IL code is the same
(release config) for both subs, both /must/ take the same time (in theory),
so the 1 second for such a long loop - I made a shorter loop and used the
StopWatch, as Göran did - can vary for any reason.
Armin
Aug 9 '07 #9

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

Similar topics

30
by: John Bailo | last post by:
The c# *return* statement has been bothering me the past few months. I don't like the fact that you can have different code paths in a method and have multiple return statements. To me, it...
3
by: Test | last post by:
Sorry, some people may have asked this question before. It is really hard to find relevant articles about this topic on the web using key words. I know it is not recommended to use return...
4
by: bluedolphin | last post by:
This is my first function in Visual and I'm having a simple syntax syntax issue that I'm hoping someone can help correct for me. I have a function Public Function...
10
by: LaEisem | last post by:
On-the-job, I have "inherited" a lot of old C language software. A question or two about when "casting" of null pointer constants is needed has occurred during behind-the-scenes cleanup of some...
3
by: Marlene Stebbins | last post by:
My program has code like this: bigint bigSub(bigint minuend, bigint subtrahend) { bigint bigdiff; bigInit(&bigdiff); if(((cmp_ops(minuend.number, subtrahend.number))==1) && minuend.sign...
15
by: Greenhorn | last post by:
Hi, when a function doesn't specify a return type ,value what value is returned. In the below programme, the function sample()is returning the value passed to 'k'. sample(int); main() { int...
6
by: lovecreatesbeauty | last post by:
I ever missed a `return' statement when write a function `int HighDigit(Num)' to get the highest digit of an integer. But even if the `return' statement is ignored the function still can obtain...
15
by: Nerox | last post by:
Hi, If i write: #include <stdio.h> int foo(int); int main(void){ int a = 3; foo(a); }
13
by: jimjim | last post by:
Hello all, I ve come across the following code fragment and I was wondering why is the copy ctr called on return (rather than just returning the string statement obj. TIA string...
7
by: Terry Olsen | last post by:
How do I get this to work? It always returns False, even though I can see "This is True!" in the debug window. Do I have to invoke functions differently than subs? Private Delegate Function...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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,...
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.