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 6083033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:21:21+00:00 2026-05-23T11:21:21+00:00

I needed to check performance of some .NET API, and I came up with

  • 0

I needed to check performance of some .NET API, and I came up with this code.

Stopwatch sw = Stopwatch.StartNew();
for (int i = 0; i < 1000; i++) {
    int res = (int) SOME .NET FUNCTION TO RUN;
}
sw.Stop();
double time = sw.Elapsed.TotalMilliseconds;

On my second thought, I was afraid that the compiler optimizes out all the operation as the res is not used any where. And I modified the code as follows.

Stopwatch sw = Stopwatch.StartNew();
long sumIt = 0;
for (int i = 0; i < 1000; i++) {
    int res = (int) SOME .NET FUNCTION TO RUN;
    sumIt += res;
}
sw.Stop();
double time = sw.Elapsed.TotalMilliseconds;
value = (int) sumIt / 1000;

Interestingly, it doesn’t seem that the compiler doesn’t optimize out the operation with my first example. I tested with csc (Visual Studio), and mono.

Here comes my questions.

  • Is it safe to make a benchmark as my first example?
  • Isn’t C# compiler smart enough to understand that some of the code can be optimized out? Or, is there any compiler parameter for that?

ADDED

The SOME FUNCTION TO RUN is actually SOME .NET FUNCTION TO RUN, and I modified the OP.
And based on the answers, it looks like that the C# compiler cannot(or doesn’t) optimize the operation as the .NET FUNCTION TO RUN might have the side effect.

  • 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-23T11:21:21+00:00Added an answer on May 23, 2026 at 11:21 am

    Compiler (JIT) may optimize whole function call out if it finds out that it has no side effects. It likely need to be able to inline function to detect that.

    I tried small function that only acts on input arguments and see it is optimized out by checking resulting assembly (make sure to try Release build with “Suppres optimization on module load” unchecked).

     ...
     for (int i = 0; i < 1000; i++) 
     {    
        int res = (int) Func(i);
     }
     ...
    
     static int Func(int arg1)
     {
        return arg1 * arg1;
     }
    

    Disassembly:

          for (int i = 0; i < 1000; i++) 
        00000016  xor         eax,eax 
        00000018  inc         eax 
        00000019  cmp         eax,3E8h 
        0000001e  jl          00000018 
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was working with ArrayWritable , at some point I needed to check how
Which is, on average, faster - check the value then, if needed, assign, or
I needed a function that simply checks if a string can be converted to
For some time now I've been structuring my code around methods with no side-effects
This is relating to the following: (In Python Code) for i in object: doSomething(i)
First of all I know this is a duplicated question and I check out
I needed to check the memory stats of objects I use in python. I
This is something I needed to clear up awhile back. In PHP 5.3+, I
I needed to check if a picture is included in a big set of
I needed to check if a BigDecimal instance is a multiple of another BigDecimal

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.