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

  • SEARCH
  • Home
  • 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 712803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:53:20+00:00 2026-05-14T04:53:20+00:00

(background: Why should I use int instead of a byte or short in C#

  • 0

(background: Why should I use int instead of a byte or short in C#)

To satisfy my own curiosity about the pros and cons of using the "appropriate size" integer vs the "optimized" integer i wrote the following code which reinforced what I previously held true about int performance in .Net (and which is explained in the link above) which is that it is optimized for int performance rather than short or byte.

DateTime t;
long a, b, c;

t = DateTime.Now;
for (int index = 0; index < 127; index++)
{
    Console.WriteLine(index.ToString());
}           
a = DateTime.Now.Ticks - t.Ticks;

t = DateTime.Now;
for (short index = 0; index < 127; index++)
{
    Console.WriteLine(index.ToString());
}
        
b=DateTime.Now.Ticks - t.Ticks;

t = DateTime.Now;           
for (byte index = 0; index < 127; index++)
{
    Console.WriteLine(index.ToString());
}
c=DateTime.Now.Ticks - t.Ticks;

Console.WriteLine(a.ToString());
Console.WriteLine(b.ToString());
Console.WriteLine(c.ToString());

This gives roughly consistent results in the area of…

~950000

~2000000

~1700000

Which is in line with what i would expect to see.

However when I try repeating the loops for each data type like this…

t = DateTime.Now;
for (int index = 0; index < 127; index++)
{
    Console.WriteLine(index.ToString());
}
for (int index = 0; index < 127; index++)
{
    Console.WriteLine(index.ToString());
}
for (int index = 0; index < 127; index++)
{
    Console.WriteLine(index.ToString());
}
a = DateTime.Now.Ticks - t.Ticks;

The numbers are more like…

~4500000

~3100000

~300000

Which I find puzzling. Can anyone offer an explanation?

NOTE:
In the interest of comparing like for like i’ve limited the loops to 127 because of the range of the byte value type.
Also this is an act of curiosity not production code micro-optimization.

  • 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-14T04:53:21+00:00Added an answer on May 14, 2026 at 4:53 am

    First of all, it’s not .NET that’s optimized for int performance, it’s the machine that’s optimized because 32 bits is the native word size (unless you’re on x64, in which case it’s long or 64 bits).

    Second, you’re writing to the console inside each loop – that’s going too be far more expensive than incrementing and testing the loop counter, so you’re not measuring anything realistic here.

    Third, a byte has range up to 255, so you can loop 254 times (if you try to do 255 it will overflow and the loop will never end – but you don’t need to stop at 128).

    Fourth, you’re not doing anywhere near enough iterations to profile. Iterating a tight loop 128 or even 254 times is meaningless. What you should be doing is putting the byte/short/int loop inside another loop that iterates a much larger number of times, say 10 million, and check the results of that.

    Finally, using DateTime.Now within calculations is going to result in some timing “noise” while profiling. It’s recommended (and easier) to use the Stopwatch class instead.

    Bottom line, this needs many changes before it can be a valid perf test.


    Here’s what I’d consider to be a more accurate test program:

    class Program
    {
        const int TestIterations = 5000000;
    
        static void Main(string[] args)
        {
            RunTest("Byte Loop", TestByteLoop, TestIterations);
            RunTest("Short Loop", TestShortLoop, TestIterations);
            RunTest("Int Loop", TestIntLoop, TestIterations);
            Console.ReadLine();
        }
    
        static void RunTest(string testName, Action action, int iterations)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            for (int i = 0; i < iterations; i++)
            {
                action();
            }
            sw.Stop();
            Console.WriteLine("{0}: Elapsed Time = {1}", testName, sw.Elapsed);
        }
    
        static void TestByteLoop()
        {
            int x = 0;
            for (byte b = 0; b < 255; b++)
                ++x;
        }
    
        static void TestShortLoop()
        {
            int x = 0;
            for (short s = 0; s < 255; s++)
                ++x;
        }
    
        static void TestIntLoop()
        {
            int x = 0;
            for (int i = 0; i < 255; i++)
                ++x;
        }
    }
    

    This runs each loop inside a much larger loop (5 million iterations) and performs a very simple operation inside the loop (increments a variable). The results for me were:

    Byte Loop: Elapsed Time = 00:00:03.8949910
    Short Loop: Elapsed Time = 00:00:03.9098782
    Int Loop: Elapsed Time = 00:00:03.2986990

    So, no appreciable difference.

    Also, make sure you profile in release mode, a lot of people forget and test in debug mode, which will be significantly less accurate.

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

Sidebar

Related Questions

Is background worker a thread? When should I use it?
First, I should be clear by providing a little background: I'm using Eclipse to
The background should be transparent, but the text should not.
Background: At my company we are developing a bunch applications that are using the
Background I am writing and using a very simple CGI-based (Perl) content management tool
Background: Over the next month, I'll be giving three talks about or at least
Question's Background: void dash(int *n, char c) is to draw characters c separated by
I'm trying to do a printable component (an invoice document). I use JComponent instead
I have a panel with transparent background which i use to draw an image.
Background: I'm using Google's protobuf , and I would like to read/write several gigabytes

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.