?
Solved

Performance of implicit vs explicit conversion.

Posted on 2003-03-28
6
Medium Priority
?
442 Views
Last Modified: 2008-03-06
Is there any performance gain/loss between using implicit vs explicit conversions?

Which of these code sections will perform more efficiently, a or b?

Section 'a' will perform an explicit conversion of CLng(y) + 100 to a string, while section 'b' will allow visual basic to do an implicit conversion. I had heard years ago that explicit conversions are faster than implicit.

dim x as string
dim y as string

y = "100"

' a
x = CStr(CLng(y) + 100)

' b
x = CLng(y) + 100
0
Comment
Question by:mferbert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 4

Expert Comment

by:SSSoftware
ID: 8228742
to mferbert,

You can answer th question yourself.

Do each version of the code in a loop of 100,000 times
 (or some value) and time it.

0
 
LVL 4

Expert Comment

by:SSSoftware
ID: 8228746
Woops,

I forgot to mention you may have to be carefull of some optimization that the compiler may perform on the code inside a loop.

I don't think VB performs any great amount of optimization
but I could be wrong.

Ed
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8229484
First: Don't use Y as string, you don't need it so.
Implicit always is better. Same is true when you declare variables, try to use an specific type.
What i would do in your code would be:

dim x as long
dim y as long
dim result as string

y = 100
x=y+100
result=cstr(x)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Expert Comment

by:Jzaltheral
ID: 8230492
Explicit is always better, but not necessarily because one way is faster.  It's better because it places exact control of what happens in your application in your hands, instead of letting the application make those decisions for you.

Just as its always better to declare your variables (and to force yourself to do use Option Explicit in every module), as this saves memory in your application, it is always better to explicitly cast your variables when you need to convert them to another type. Explicity converting them insures you get the results you were looking for, instead of having to hunt down an implicit casting reference error.  In a small app, that's no big deal, but imagine having to sort through 10,000 lines of code looking for one implicit coversion that strips off a decimal you need.  I think you'll get the picture.


Jzal
0
 

Expert Comment

by:CleanupPing
ID: 8531382
Hi mferbert,
This old question (QID 20567015) needs to be finalized -- accept an answer, split points, or get a refund.  Please see http://www.cityofangels.com/Experts/Closing.htm for information and options.
0
 

Accepted Solution

by:
YensidMod earned 0 total points
ID: 9496263
This question is PAQed and no points refunded (of 250)

YensidMod
Expert Exchange Moderator
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question