locked
using short or byte instead of int RRS feed

  • Question

  • User1655654435 posted

    I have a list of database tables where each table contains alot of small columns of a number type:

    public int answer1 {get;set;}
    public int answer2 {get;set;}
    public int answer3 {get;set;}
    ...etc

    the data that is stored in these are just 3 different values. 1,2 or 0. So I'm wondering if it is better for me to use a smaller datatype here?

    But most importantly, when I'm calculating the answers, i'm wondering if i can get a performance benefit to changing them to short or byte because i have to iterate through them.

    for example this is my current way of doing it:

    int[] yesAnswers = new int[49];
    int[] noAnswers = new int[49];
    //puts all answers from a db table into an array that is 49 numbers long.
    int[] answers = PopulateFromDb(); for (int i = 0; i < answers.Length; i++)
    {
    if (answers[i] == 1)
    {
    yesAnswers[i] += 1;
    }
    else if (answers[i] == 2)
    {
    noAnswers[i] += 1;
    }
    }

    if i were to change to a smaller datatype, would it make a difference if i had a lot of such calculations?

    Friday, February 1, 2019 9:57 AM

Answers

  • User-943250815 posted

    I don´t think change from int to short or byte will make any difference.

    C# language spec 7.2.6.1 - Unary numeric promotion:
    Unary numeric promotion occurs for the operands of the predefined +, –, and ~ unary operators. Unary numeric promotion simply consists of converting operands of type sbyte, byte, short, ushort, or char to type int. Additionally, for the unary – operator, unary numeric promotion converts operands of type uint to type long.

    https://download.microsoft.com/download/3/8/8/388e7205-bc10-4226-b2a8-75351c669b09/csharp language specification.doc

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 1, 2019 11:03 AM
  • User1520731567 posted

    Hi bluMarmalade,

    But most importantly, when I'm calculating the answers, i'm wondering if i can get a performance benefit to changing them to short or byte because i have to iterate through them.

    Not much difference.

    If you want to improve performance in asp.net,

    here are a few tips to improve the performance of your ASP.Net application:

    1.Viewstate. 
    2.Avoid Session and Application Variables. 
    3.Use Caching. 
    4.Effectively use CSS and Script files. 
    5.Images sizes. 
    6.CSS based layout. 
    7.Avoid Round trips. 
    8.Validate using JavaScript.

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 4, 2019 5:27 AM
  • User-943250815 posted

    About best performance there is no magic solution, you have to try some different scenarios for your case and test, to see what best fit for your case.
    It can be real good for now, and 6 months later not, due to increased volume.
    You can buy a truck to transport anything, but some day a customer contract you to deliver flowers next day on morning, but your truck is not fast enough to do it, you can deliver only at lunch....

    So you can made your calc, and keep storing in Array, List, Database Table, Text File, whatever you want.
    Which is best, the one that give a better balance between used resources and time of response.
    So have in mind an Array should be expanded, or a List should have a capacity expanded, these operations take time, on the other hand write to a Table or Text file take some Disk IO time.

    Also there are situations that you need to split your job, and repeat same task many times as needed.
    Back to truck example, your customer asked to deliver 20tons but your truck can only load 7 tons, you will have 3 trips.....

    I see no way other than test!
    You can elect one strategy that is real good now, and later, you have to review

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 17, 2019 5:20 PM

All replies

  • User-943250815 posted

    I don´t think change from int to short or byte will make any difference.

    C# language spec 7.2.6.1 - Unary numeric promotion:
    Unary numeric promotion occurs for the operands of the predefined +, –, and ~ unary operators. Unary numeric promotion simply consists of converting operands of type sbyte, byte, short, ushort, or char to type int. Additionally, for the unary – operator, unary numeric promotion converts operands of type uint to type long.

    https://download.microsoft.com/download/3/8/8/388e7205-bc10-4226-b2a8-75351c669b09/csharp language specification.doc

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 1, 2019 11:03 AM
  • User1520731567 posted

    Hi bluMarmalade,

    But most importantly, when I'm calculating the answers, i'm wondering if i can get a performance benefit to changing them to short or byte because i have to iterate through them.

    Not much difference.

    If you want to improve performance in asp.net,

    here are a few tips to improve the performance of your ASP.Net application:

    1.Viewstate. 
    2.Avoid Session and Application Variables. 
    3.Use Caching. 
    4.Effectively use CSS and Script files. 
    5.Images sizes. 
    6.CSS based layout. 
    7.Avoid Round trips. 
    8.Validate using JavaScript.

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 4, 2019 5:27 AM
  • User1655654435 posted

    Actually I've discovered the best solution to my problem for scalability would be to use Span<T>, where i can use it on the arrays without taking up any more resources. That being said, there aren't alot of good tutorials on how to use it properly outside of very simple usage cases

    Monday, February 4, 2019 2:04 PM
  • User303363814 posted

    Just a thought … are you sure you are making the best use of your time?  Do you actually have a problem?  Or just think that you might have one in the future?  Whoever is paying for the application is not going to care if you save a few megabytes here or there (after all the smallest computer that they are carrying around with them in their pocket probably has at least 8000 megabytes, the server that the code will run on will hive tens of thousands of megabytes - save 50? who cares?).

    They will care about actually performance as seen by the users.  As always, Eric Lippert speaks sensibly

    https://ericlippert.com/2012/12/17/performance-rant/

    Monday, February 4, 2019 9:35 PM
  • User-1885736663 posted

    Surely it makes lot of difference with respect to performance.

    Since byte occupies only 8 bits.

    but int occupies 32 bits.

    See the difference in memory usage. Memory is always the bottleneck for the computer.

    Tuesday, February 5, 2019 6:47 PM
  • User303363814 posted

    Since byte occupies only 8 bits.

    Does it?  On a 64 bit computer?  With memory alignment optimised for speed?

    but int occupies 32 bits.

    See the difference in memory usage. Memory is always the bottleneck for the computer.

    Wow!  That is a very, very broad assertion.  Do you have any proof for always

    Interesting that the first paragraph of the experts comment on this sort of thing is

    If you have two horses and you want to know which of the two is the faster then race your horses. Don’t write short descriptions of the horses, post them on the Internet, and ask random strangers to guess which is faster! Even if by sheer chance you got an accurate answer, how would you have any confidence in its accuracy? You can easily and accurately discover which of two programs is faster by running both yourself and measuring them with a stopwatch.

    (As an aside - the arrays are less than 100 entries.  I will have spent more time typing this sentence than it will take to run those loops a million times over.  Are you sure you are not wasting time by even asking the question.  If you care - measure, measure, measure.  Don't ask random people (me included) on the internet. My 2c)

    Thursday, February 7, 2019 12:31 AM
  • User-1885736663 posted

    Yes C# is system architecture neutral.

    Kindly refer

    https://stackoverflow.com/questions/1101463/the-double-byte-size-in-32-bit-and-64-bit-os

    Thursday, February 7, 2019 2:31 AM
  • User1655654435 posted

    i'm interested in why, not just measuring if something is faster. I got some kind of answer to that, in that it makes little difference and infact probably ints are faster for the most part. But I am interesting in high performance scenarios where something could be fast but still eat alot of memory which could be avoided for instance by using span and/or byte/short etc

    Sunday, February 17, 2019 2:12 PM
  • User-943250815 posted

    About best performance there is no magic solution, you have to try some different scenarios for your case and test, to see what best fit for your case.
    It can be real good for now, and 6 months later not, due to increased volume.
    You can buy a truck to transport anything, but some day a customer contract you to deliver flowers next day on morning, but your truck is not fast enough to do it, you can deliver only at lunch....

    So you can made your calc, and keep storing in Array, List, Database Table, Text File, whatever you want.
    Which is best, the one that give a better balance between used resources and time of response.
    So have in mind an Array should be expanded, or a List should have a capacity expanded, these operations take time, on the other hand write to a Table or Text file take some Disk IO time.

    Also there are situations that you need to split your job, and repeat same task many times as needed.
    Back to truck example, your customer asked to deliver 20tons but your truck can only load 7 tons, you will have 3 trips.....

    I see no way other than test!
    You can elect one strategy that is real good now, and later, you have to review

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 17, 2019 5:20 PM