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

fastest way to change type

which is the fastest or best out of these

Convert.ToInt32();
(int)x;
int.Parse(x);
Thanks,
yurps

Nov 19 '05 #1
4 984
yurps wrote:
which is the fastest or best out of these

Convert.ToInt32();
(int)x;
int.Parse(x);
Thanks,
yurps


It depends on what you want to convert it FROM.
(int)x doesn't work for a string-x, int.Parse
*only* works on strings.

You could try it yourself with a small program:
- Remember System.DateTime.Now
- do the conversion a lot of times (100.000)
- get a fresh DateTime.Now
- subtract the times.
Repeat a couple of times to get an average value.

Repeat again for an other conversion.
--
Hans Kesting
Nov 19 '05 #2
I refuse to answer this question for two reasons

First, not all methods do the same thing

string str = "1";
int i = Convert.ToInt32(str ); //this will work
int i = (int) str //this won't work

so you aren't comparing apples to apples

secondly, any difference would be so absolutely minor and any major
differences would be so specific to a given situation, no one could
accurately answer save the person asking...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"yurps" <yu***@yahoo.co.uk> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
which is the fastest or best out of these

Convert.ToInt32();
(int)x;
int.Parse(x);
Thanks,
yurps

Nov 19 '05 #3
ok how about int.Parse(x) and Convert.ToInt32(x) ???

Nov 19 '05 #4

int.Parse only accepts strings

Convert.ToInt32() accepts more type

when passing in a string, Convert.ToInt32() simply calls int.Parse...it's
actually a bit slower, because it does a null check...but that also makes it
100% better to use if you ask me...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"yurps" <yu***@yahoo.co.uk> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
ok how about int.Parse(x) and Convert.ToInt32(x) ???

Nov 19 '05 #5

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

Similar topics

11
by: hoopsho | last post by:
Hi Everyone, I am trying to write a program that does a few things very fast and with efficient use of memory... a) I need to parse a space-delimited file that is really large, upwards fo a...
8
by: Phil_Harvey | last post by:
I need to know how I scroll to a particular element with javascript. <label>&gt; <input name="tf_command" size="50" type="text"></label> for example I want the screen to scroll to this input box...
3
by: _DD | last post by:
I had one experimented with binary serialization of an ArrayList of structs (each struct mostly contains strings). Strangely enough, it did not run as fast as custom XML storage (latter was...
22
by: SETT Programming Contest | last post by:
The SETT Programming Contest: The fastest set<Timplementation Write the fastest set<Timplementation using only standard C++/C. Ideally it should have the same interface like std::set. At least...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
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...
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
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,...

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.