Performance of implicit vs explicit conversion.

Posted on 2003-03-28
Medium Priority
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
Question by:mferbert

Expert Comment

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.


Expert Comment

ID: 8228746

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.

LVL 16

Expert Comment

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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.


Expert Comment

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.


Expert Comment

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.

Accepted Solution

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

Expert Exchange Moderator

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

600 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