8 Replies - 1018 Views - Last Post: 24 April 2011 - 03:38 AM

#1 FreezingHazard  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 148
  • Joined: 22-February 10

Performance gains from using Integer's precisely?

Posted 25 April 2010 - 01:21 PM

Is there any performance gains from using integral type closely related to it's data?

I mean if I got a number between 1 and 5 why not use a short, byte, or even sbyte instead of standard Int32?
Is This A Good Question/Topic? 0
  • +

Replies To: Performance gains from using Integer's precisely?

#2 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: Performance gains from using Integer's precisely?

Posted 25 April 2010 - 01:56 PM

You can always test them out yourself. In C# use stopwatch to measure time and see try different types. Of course it should be a bit faster to use byte instead of an integer but that advantage might be lost when you call some function with the byte that takes an integer: in worst case you might have to convert that byte to integer when you call the function and that would be slower than just using integer.


I made a small test snippet:
        public static void Main(string[] args)
        {
            long[] testResults = new long[10];
            Stopwatch st = new Stopwatch();
            for (int i = 0; i < testResults.Length; i++) {
                st.Start();
                for (int j = 0; j < 10000000; j++) {
                    long xxx = 1; // change to desired type
                }
                testResults[i] = st.ElapsedTicks;
                Console.WriteLine(testResults[i]);
                st.Reset();
            }
            double average = testResults.Average();
            Console.WriteLine("avg: " + average);
            Console.ReadKey();
        }



From byte to int the results seemed to be about 75.000 ticks for 10.000.000 iterations but for long it seemed to be a bit over 80k. With no initialization at all each iteration took about 65k ticks. So not much difference there. Of course the snippet might not be very accurate as it does nothing: the compiler might do something with it behind the scenes if it's smart enough (though optimization checkbox wasn't checked).

If a program is slow for some reason, replacing integers with bytes would be the last thing I'd do to speed it up.
Was This Post Helpful? 2
  • +
  • -

#3 FreezingHazard  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 148
  • Joined: 22-February 10

Re: Performance gains from using Integer's precisely?

Posted 25 April 2010 - 02:13 PM

Hm well thanks..

I was mainly wondering perhaps data types allocate space when getting used. So Int32 would be slower than a byte or sbyte.
Was This Post Helpful? 0
  • +
  • -

#4 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Performance gains from using Integer's precisely?

Posted 25 April 2010 - 03:25 PM

It's pretty hard to tell unless you want to study the generated instructions, study the processor, its architecture, and a number of other things. The answer is that it really doesn't matter.

Using shorts saves you memory, sure, and that can make a difference if you're packing a lot of them (a very large array), quite obviously. It's no so clear what the implications are on a single short. Besides, it's not that simple. Processors are designed to work with datatypes of their architecture size. So 32 bits or 64 bits for you. So you might see your datatype being padded and things like that.

What's more important is correctness. Are you sure you won't run into overflow if you use shorts? 100% sure?

This post has been edited by Oler1s: 25 April 2010 - 03:26 PM

Was This Post Helpful? 1
  • +
  • -

#5 FreezingHazard  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 148
  • Joined: 22-February 10

Re: Performance gains from using Integer's precisely?

Posted 25 April 2010 - 04:31 PM

I just did a test doing a loop 50 million times for each sbyte, byte, short, and int32.

The test called the data type and changed its values 4 times.

Averages are as follows
#1 Short
#2 Int32
#3 Sbyte
#4 Byte

The difference between Short and Int32 was usually by 40ms or so and Sbyte shadowing behind or next to Int32 all the time.

Byte however was behind 100ms
Was This Post Helpful? 0
  • +
  • -

#6 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Performance gains from using Integer's precisely?

Posted 25 April 2010 - 04:52 PM

You run your test multiple times at different times? All you need is the processor to schedule your thread a bit differently, because another thread takes up a time slice, etc.
Was This Post Helpful? 0
  • +
  • -

#7 FreezingHazard  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 148
  • Joined: 22-February 10

Re: Performance gains from using Integer's precisely?

Posted 25 April 2010 - 05:09 PM

Individual..
Was This Post Helpful? 0
  • +
  • -

#8 Galois  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 207
  • Joined: 27-March 10

Re: Performance gains from using Integer's precisely?

Posted 25 April 2010 - 05:54 PM

View PostFreezingHazard, on 25 April 2010 - 12:21 PM, said:

Is there any performance gains from using integral type closely related to it's data?


Most likely. But not anything that somebody using C# should be worrying about.
Was This Post Helpful? 0
  • +
  • -

#9 The Architect 2.0  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 351
  • Joined: 22-May 08

Re: Performance gains from using Integer's precisely?

Posted 24 April 2011 - 03:38 AM

i always thought it was 'less taxing' on the system to use variable types that are multiple of the system's word size
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1