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

VB.Net CType vs. Convert & C# Casting vs. Convert

It's official....Convert blows.

I ran a number of tests converting a double to an integer usingboth Convert & CType. I *ASSUMED* that CType would piggy-back ontop of Convert, and that performance would be identical.

I was 100% incorrect.

The code below produces the results:
CType Took: 0.2187528 seconds.
Convert Took: 12.187656 seconds.

Option Explicit On
Option Strict On

Module ConversionTest

Private Const STORED_VALUE As Double = 100000000
Sub Main()

TestCType()
TestConvert()

Console.ReadLine()
End Sub

Private Sub TestCType()
Dim l_Time As Long = 0
Dim l_Converted As Integer = 0

l_Time = DateTime.Now.Ticks
For l_LoopCounter As Integer = 0 To 100000000
l_Converted = CType(STORED_VALUE, Integer)
Next
l_Time = DateTime.Now.Ticks - l_Time
Console.WriteLine("Took " & (l_Time / 10000000) & "seconds.")

End Sub
Private Sub TestConvert()
Dim l_Time As Long = 0
Dim l_Converted As Integer = 0

l_Time = DateTime.Now.Ticks
For l_LoopCounter As Integer = 0 To 100000000
l_Converted = Convert.ToInt32(STORED_VALUE)
Next
l_Time = DateTime.Now.Ticks - l_Time
Console.WriteLine("Took " & (l_Time / 10000000) & "seconds.")
End Sub

End Module

The IL is VERY different. It appears as though the Convert iscreating a local copy of the Const variable per loop. Is thiscorrect? Does Convert take into account globalization? (Thatwould certainly explain the delay) If you change the conversionto act against the l_LoopCounter then the performance differencenarrows substantially, BUT Convert is still far behind.

It seems that in *ALL* cases, CType > Convert. (This *ALSO* holdstrue in C#.)

Thank you.
--------------------------------
From: Logan X

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>F4QPoBi5CkGScnFqZ4CGOw==</Id>
Nov 22 '05 #1
2 14559
Logan X via .NET 247 <an*******@dotnet247.com> wrote:
It's official....Convert blows.

I ran a number of tests converting a double to an integer using both
Convert & CType. I *ASSUMED* that CType would piggy-back on top of
Convert, and that performance would be identical.


They're doing different things though. For example, Convert.ToInt32
performs bankers rounding, whereas casting truncates towards 0. Here's
an example:

using System;

class Test
{
static void Main()
{
Show(0.5);
Show(1.5);
Show(-0.5);
Show(-1.5);
}

static void Show (double d)
{
Console.WriteLine ("{0}\t{1}\t{2}",
d, (int)d, Convert.ToInt32(d));
}
}

Bankers rounding requires actual calculations, as opposed to truncation
which I suspect can be done in a single processor instruction. (Haven't
checked that, but I suspect it can.)

Convert.ToInt32 also always detects overflow and throws an exception if
it occurs.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 22 '05 #2
Logan X via .NET 247 <an*******@dotnet247.com> wrote:
It's official....Convert blows.

I ran a number of tests converting a double to an integer using both
Convert & CType. I *ASSUMED* that CType would piggy-back on top of
Convert, and that performance would be identical.


They're doing different things though. For example, Convert.ToInt32
performs bankers rounding, whereas casting truncates towards 0. Here's
an example:

using System;

class Test
{
static void Main()
{
Show(0.5);
Show(1.5);
Show(-0.5);
Show(-1.5);
}

static void Show (double d)
{
Console.WriteLine ("{0}\t{1}\t{2}",
d, (int)d, Convert.ToInt32(d));
}
}

Bankers rounding requires actual calculations, as opposed to truncation
which I suspect can be done in a single processor instruction. (Haven't
checked that, but I suspect it can.)

Convert.ToInt32 also always detects overflow and throws an exception if
it occurs.

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

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

Similar topics

1
by: Logan X via .NET 247 | last post by:
It's official....Convert blows. I ran a number of tests converting a double to an integer usingboth Convert & CType. I *ASSUMED* that CType would piggy-back ontop of Convert, and that performance...
10
by: Douglas Buchanan | last post by:
I am using the following code instead of a very lengthly select case statement. (I have a lot of lookup tables in a settings form that are selected from a ListBox. The data adapters are given a...
4
by: Mike Cooper | last post by:
There is something about inherited classes I evidently don't know... I wrote the following class: Class Class1 inherits System.Windows.Forms.DataGridTextBoxColumn End Class There is...
4
by: Fredrik Melin | last post by:
I have the following code that I get "Cannot convert from DacsaSupport.Inventory to DacsaNET.Inventory" Public Function RetrieveItem(ByVal Product_ID As String) As Inventory Dim...
6
by: Ot | last post by:
I apparently have a bit to learn about Casting and Conversion. I have been thinking of them as the same but a discussion in another thread leads me to believe that this is wrong thinking. I...
8
by: John A Grandy | last post by:
could someone please discuss the pros and cons of CType(MyDouble,Decimal) versus Convert.ToDecimal(MyDouble) .... and other such conversions ...
7
by: Brian Henry | last post by:
is there any speed diffrences between doing Ctype or directcast? I know about the inherite diffrences, but process usage time wise, does one take up more cycles then the other? thanks
3
by: FB's .NET Dev PC | last post by:
I'm trying to create a general-purpose routine to update a value in a data location on a remote server. The client-server protocol is OPC, but that isn't immediately important to my question. The...
8
by: =?Utf-8?B?d2lubGlu?= | last post by:
Hello Everything in .Net is an object and CTYPE converts one datatype to another datatype so why use Cstr, Cint or any other method to convert from one datatype to another?
8
by: Joe HM | last post by:
Hello - I was wondering that the "cleanest" way is to determine whether a CType() will throw an InvalidCastException? I have data I receive as an Object and I want to convert it to a String...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.