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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:00:21+00:00 2026-06-19T00:00:21+00:00

As I am calling ummanaged dll from C#, I’ve gone through some testing about

  • 0

As I am calling ummanaged dll from C#, I’ve gone through some testing about the performance of for-loop in C# and C…

The result amazed me in the way that as the loop goes over bigger range, the performance of C# decreases as compared to C..For smaller range,C# shown well performance over C….But, as upper range of for-loop increases, the C# performance degrades as compare to C….

Here is my testing code….

    [DllImport("Testing.dll", CallingConvention =  CallingConvention.Cdecl)]
    public static extern int SumLoop(int lowLimit, int highLimit);

    public static void Main(string[] args)
    {
        const int LowerRange = 1;
        const int HigherRange = 1000000;

        // Test with C# For Loop
        var watch1 = new Stopwatch();
        watch1.Start();
        int sum = 0;
        for (int i = LowerRange; i <= HigherRange; i++)
        {
            sum += i;
        }
        watch1.Stop();

        long elapseTime1 = watch1.ElapsedMilliseconds;

        // Test with C-for loop
        var watch2 = new Stopwatch();
        watch2.Start();
        int sumFromC = SumLoop(LowerRange , HigherRange);
        long elapseTime2 = watch2.ElapsedMilliseconds;
   }

Testing.dll:

__declspec(dllexport) int SumLoop(int lowLimit, int highLimit)
{
    int idx;
    int totalSum = 0;
    for(idx = lowLimit;idx<= highLimit; idx= idx +1)
    {
        totalSum += idx;
    }
    return totalSum;
}

Testing Result :

Testing 1 :

HigherRange : 1000000

C# Loop : 4 millisecond

C-loop : 9 millisecond

Testing 2 :

HigherRange : 10000000

C# Loop : 53 millisecond

C-loop : 36 millisecond

Testing 3 :

HigherRange : 100000000

C# Loop : 418 millisecond

C-loop : 343 millisecond

Here, I started above testing with the aim that C for-loop performance will be better than C# loop but it goes exactly opposite with my understanding and gone with this question and agreed…But when I increase the upper range of for loop, the C performance goes well as compared to C#…

Now,I’m thinking that is the appraoch of testing is wrong or is it the expected performance result?

  • 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-06-19T00:00:22+00:00Added an answer on June 19, 2026 at 12:00 am

    What’s happening here is that you are ignoring the fixed overhead of using P/Invoke to call the C function.

    The C function will be faster than the C# version BUT because of the relatively large overhead of calling it, the C function will appear to be slower for small arrays because the calling overhead is a relatively large proportion of the overall time.

    However, as you increase the size of the collection the overhead will become a smaller and smaller proportion of the overall time, until the extra speed of the C version asserts itself and you start to see it running faster.

    If you look at the times for the C# function, you can see that it is indeed increasing more or less linearly with N, which you’d expect. Compare T = 4 with T = 418 after you increase N by a factor of 100. Just what you’d expect. But the C times do NOT appear to increase linearly, for the reason outlined above.

    Incidentally, if you take at least two timings, you can use simultaneous equations to solve:

    T = K + XN
    

    Where K is the fixed overhead, and X is the overhead per element.

    I have calculated from your timings that the fixed overhead for calling the unmanaged code is approximately 5.6 milliseconds and the overhead per element is 3.373737 x 10^-6 milliseconds.

    That overhead seems somewhat large, but I guess there’s some inaccuracies in the measured data.

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

Sidebar

Related Questions

So, I am calling a function from an unmanaged .dll file from my C#
When calling an unmanaged Dll from a c# application I get an AccessViolationException .
I am currently running into an issue of calling an Win32 DLL[Native] from a
I have some C code that compiles to a DLL. From C#, I need
Still working on a problem that started from here Calling C++ dll function from
I have an unmanaged DLL which I'm currently calling from C# using a COM
I have .NET assembly that calling functions from unmanaged DLL written in C (without
Here's my problem: I have an unmanaged dll that I made.I'm calling one of
Calling the WriteObject Method from a background thread is not possible! Is there a
Calling TextRenderer.MeasureText as follows: TextRenderer.MeasureText(myControl.Text, myControl.Font); and comparing the result to the size of

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.