What is fastest: (int), Convert.ToInt32(x) or Int32.Parse(x)?

后端 未结 15 1166
南笙
南笙 2021-02-02 06:13

Which of the following code is fastest/best practice for converting some object x?

int myInt = (int)x;

or

int myInt = Convert.T         


        
15条回答
  •  走了就别回头了
    2021-02-02 06:27

    this is not true. The fast conversion is direct cast:

    int i = (int) stringData;
    
    watch.Elapsed = {00:00:00.1732388}
    watch2.Elapsed= {00:00:00.0878196}
    
    
     // Mesary start
                    Stopwatch watch = new Stopwatch();
    
                    watch.Start();
                    for (int f = 1; f < 1000000; f++)
                    {
                        item.Count = FastInt32.IntParseFast(dt.Rows[i]["TopCount"]);
                    }   // Execute the task to be timed
                    watch.Stop();
    
                    Console.WriteLine("Elapsed: {0}", watch.Elapsed);
                    Console.WriteLine("In milliseconds: {0}", watch.ElapsedMilliseconds);
                    Console.WriteLine("In timer ticks: {0}", watch.ElapsedTicks);
                    // Mesary end
    
    
                    // Mesary start
                    Stopwatch watch2 = new Stopwatch();
    
                    watch2.Start();
                    for (int n = 1; n < 1000000; n++)
                    {
                        item.Count = (int)(dt.Rows[i]["TopCount"]);
                    }   // Execute the task to be timed
                    watch2.Stop();
    
                    Console.WriteLine("Elapsed: {0}", watch2.Elapsed);
                    Console.WriteLine("In milliseconds: {0}", watch2.ElapsedMilliseconds);
                    Console.WriteLine("In timer ticks: {0}", watch2.ElapsedTicks);
                    // Mesary end
    

提交回复
热议问题