Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 503001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:20:58+00:00 2026-05-13T06:20:58+00:00

While developing my application i came across some comparison stuff here was it: string

  • 0

While developing my application i came across some comparison stuff here was it:

    string str = "12345";
    int j = 12345;
    if (str == j.ToString())
    {
        //do my logic
    }

I was thinking that the above stuff can also be done with:

    string str = "12345";
    int j = 12345;
    if (Convert.ToInt32(str) == j)
    {
        //do my logic
    }

So i developed a sample code to test in terms of performance which one is better

        var iterationCount = 1000000;
        var watch = new Stopwatch();
        watch.Start();
        string str = "12345";
        int j = 12345;
        for (var i = 0; i < iterationCount; i++)
        {
            if (str == j.ToString())
            {
                //do my logic
            }
        }
        watch.Stop();

And second one:

  var iterationCount = 1000000;
    var watch = new Stopwatch();
    watch.Start();
    string str = "12345";
    int j = 12345;
    for (var i = 0; i < iterationCount; i++)
    {
        if (Convert.ToInt32(str) == j)
        {
            //do my logic
        }
    }
    watch.Stop();

On running the above two tests i found the above tests were giving nearly the same time elapsed. I would like to discuss which one is the better approach? And is there any other approach better than two above two?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:20:59+00:00Added an answer on May 13, 2026 at 6:20 am

    Your test is fundamentally flawed. The compiler and run-time are really clever beasts and will optimise the code both at compile time and run time (JIT-ing). In this case, you are doing the same thing every time which will be spotted by the compiler and optimised out, hence the timing will be similar for each method.

    Try this version (I’ve only got .Net 2.0, hence the slight changes):

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Diagnostics;
    
    namespace ToStringTest
    {
        class Program
        {
            const int
                iterationCount = 1000000;
    
            static TimeSpan Test1()
            {
                Stopwatch watch = new Stopwatch();
                watch.Start();
                string str = "12345";
                int j = 12345;
                for (int i = 0; i < iterationCount; i++)
                {
                    if (str == i.ToString())
                    {
                        //do my logic
                    }
                }
                watch.Stop();
                return watch.Elapsed;
            }
    
            static TimeSpan Test2()
            {
                Stopwatch watch = new Stopwatch();
                watch.Start();
                string str = "12345";
                int j = 12345;
                for (int i = 0; i < iterationCount; i++)
                {
                    if (Convert.ToInt32(i) == j)
                    {
                        //do my logic
                    }
                }
                watch.Stop();
                return watch.Elapsed;
            }
    
            static void Main(string[] args)
            {
                Console.WriteLine("ToString = " + Test1().TotalMilliseconds);
                Console.WriteLine("Convert = " + Test2().TotalMilliseconds);
            }
        }
    }
    

    and you will see a huge difference. One is two orders of magnitude faster than the other. And it really is obvious which one it is.

    You need to know what the various operations are doing in order to know which is fundamentally faster.

    Converting a string to an int requires the following:

    total = 0
    for each character in string
      total = total * 10 + value of charater
    

    and the ToString requires:

    string = ""
    while value != 0
      string.AddToFront value % 10
      value /= 10
    

    Multiplication is far easier, and faster, for a CPU to do than division. Given the choice of an algorithm with lots of multiplies versus an algorithm with lots of divides, always go for the former as it will always be faster.

    Then there’s the comparison, an int – int comparison is simple, load each value into a register and compare – a couple of machine instructions and you’re done. A comparison between two strings requires testing each character in the strings one at a time – in the example you gave it was 5 bytes (an int is probably 4 bytes) which is more memory accesses.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 304k
  • Answers 304k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You should design your schema to track this changes instead… May 13, 2026 at 8:51 pm
  • Editorial Team
    Editorial Team added an answer you need to set the PATH environment variable of windows… May 13, 2026 at 8:51 pm
  • Editorial Team
    Editorial Team added an answer More an hint than an answer but similar Eclipse/Tomcat/Spring problems… May 13, 2026 at 8:51 pm

Related Questions

I am taking my first foray into PHP programming and need to configure the
Question How can I make sure my application is thread-safe? Are their any common
This is an issue that recently came up for me while writing a new
I've been developing modules for DNN since version 2 and back then I was
I'm developing an application, that makes use of some REST web services. It's technical

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.