none
Performance of basic operations? RRS feed

  • Question

  • I often hear that certain operations like the modulo operator % are expensive. Now I would like to have a relative comparsion of basic operations (+=, *=, /=, &=, %=,...) but wasn't able to find anything. I also wrote a short test but the for loop will make the messure probably rather vague.

    Any information on this?

     

    Tuesday, March 8, 2011 9:24 AM

Answers

  • Shamanu,

    We are not living in 1960 anymore. 

    The time spent by those operator instructions (including the modulo) are really unimportant for the normal programs which are made with C#.

    If those you mention become really important then you have to use computers with other OS systems and other hardware then those on which C# can run.

     


    Success
    Cor
    • Marked as answer by Mio_Miao Thursday, March 17, 2011 11:36 AM
    Tuesday, March 8, 2011 9:49 AM
  • Just to illustrate Cor's words, here is a quick estimate of the speed of operators on my machine:

    Integer addition/subtraction : 430 million per second
    Integer multiplication : 370 million per second
    Integer division/modulo : 150 million per second

    Double addition/subtraction: 330 million per second
    Double multiplication : 370 million per second
    Double division : 250 million per second
    Double modulo : 150 million per second

    Unless you need to compute 1 million prime numbers per second, you needn't worry about the speed of operators.

    • Proposed as answer by Mike DanesModerator Tuesday, March 8, 2011 12:41 PM
    • Marked as answer by Mio_Miao Thursday, March 17, 2011 11:35 AM
    Tuesday, March 8, 2011 11:11 AM
  • Yes but it is still nice if someone says modulo is slow and you can show him a site/book that shows it costs only ~2 additions. 
    It would be a lot better if you could show him that micromanaging in the .NET environment goes against the whole managed code concept.
    • Marked as answer by Mio_Miao Thursday, March 17, 2011 11:36 AM
    Tuesday, March 8, 2011 7:15 PM
  • Yes but it is still nice if someone says modulo is slow and you can show him a site/book that shows it costs only ~2 additions. 

    The only kind of book where you could have such a comparison would be a technical specification of your specific CPU.

    • Marked as answer by Mio_Miao Thursday, March 17, 2011 11:36 AM
    Wednesday, March 9, 2011 9:48 PM

All replies

  • Shamanu,

    We are not living in 1960 anymore. 

    The time spent by those operator instructions (including the modulo) are really unimportant for the normal programs which are made with C#.

    If those you mention become really important then you have to use computers with other OS systems and other hardware then those on which C# can run.

     


    Success
    Cor
    • Marked as answer by Mio_Miao Thursday, March 17, 2011 11:36 AM
    Tuesday, March 8, 2011 9:49 AM
  • Yes I know that this is normaly not relevant. But in rare cases where one does some million calculations to simulate something it might be noticable.

    Tuesday, March 8, 2011 10:40 AM
  • Just to illustrate Cor's words, here is a quick estimate of the speed of operators on my machine:

    Integer addition/subtraction : 430 million per second
    Integer multiplication : 370 million per second
    Integer division/modulo : 150 million per second

    Double addition/subtraction: 330 million per second
    Double multiplication : 370 million per second
    Double division : 250 million per second
    Double modulo : 150 million per second

    Unless you need to compute 1 million prime numbers per second, you needn't worry about the speed of operators.

    • Proposed as answer by Mike DanesModerator Tuesday, March 8, 2011 12:41 PM
    • Marked as answer by Mio_Miao Thursday, March 17, 2011 11:35 AM
    Tuesday, March 8, 2011 11:11 AM
  • Yes I know that this is normaly not relevant. But in rare cases where one does some million calculations to simulate something it might be noticable.

    Shamanu,

    I'm glad Louis shows it with his sample. 

    However, those million calcululations can be interupted hundred of thousand times because all Windows OS systems are Multitasking and therefore can be interupted at any moment. 

    Your simulation will then never be correct so for those cases where really absolute correct result has to be used, are Windows OS system mostly not the perfect choose. (And that is than in that small area of computers where non Windows OS systems are used)


    Success
    Cor
    Tuesday, March 8, 2011 12:21 PM
  • Unless you need to compute 1 million prime numbers per second, you needn't worry about the speed of operators.

    Yes but it is still nice if someone says modulo is slow and you can show him a site/book that shows it costs only ~2 additions. 

    I had similar results but my test was with a for loop and if the for takes more time then the addition the gap of addition vs modulo would be bigger.

    Tuesday, March 8, 2011 6:12 PM
  • Yes but it is still nice if someone says modulo is slow and you can show him a site/book that shows it costs only ~2 additions. 
    It would be a lot better if you could show him that micromanaging in the .NET environment goes against the whole managed code concept.
    • Marked as answer by Mio_Miao Thursday, March 17, 2011 11:36 AM
    Tuesday, March 8, 2011 7:15 PM
  • Yes but it is still nice if someone says modulo is slow and you can show him a site/book that shows it costs only ~2 additions. 

    The only kind of book where you could have such a comparison would be a technical specification of your specific CPU.

    • Marked as answer by Mio_Miao Thursday, March 17, 2011 11:36 AM
    Wednesday, March 9, 2011 9:48 PM