C# how to faster the for loop iteration

T.Zacks 3,986 Reputation points
2021-10-09T19:56:50.537+00:00

I am asking very basic question. i am taking about simple for loop. if my C# loop iterate 5 million time & take 5 minute to complete on a pc then when the same loop will be design with java then does it take same time ?

language to language loop iteration speed varies ? loop speed depend on CPU speed only or anything else is also responsible for it ?

for what are the factor for which loop iteration speed increase ?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,602 questions
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Lex Li (Microsoft) 5,237 Reputation points Microsoft Employee
    2021-10-09T20:21:09.323+00:00

    You'd better see it from another angle. For example,

    for (int i = 0; i < 5000000; i++) {
    // do nothing
    }
    

    clearly won't take 5 minutes on any modern machine. So the slowness comes from the statements you wrote for each iteration. To further analyze performance, you need to learn tools like performance profilers. Only when you know the actual cause of slowness, you can think of possible solutions (such as running iterations in parallel instead of in sequence).

    Keep in mind performance tuning is a huge topic, but basic principles and profilers (even Java has similar tools) apply to all cases.

    1 person found this answer helpful.

  2. P a u l 10,416 Reputation points
    2021-10-09T20:23:39.64+00:00

    It depends entirely on what's inside the loop. Loops themselves aren't inherently fast or slow on their own, and the difference between different languages is probably negligible in practical terms.
    CPU speed is also only relevant if the task occurring inside the loop is actually CPU-bound. Did you have an idea of the sort of task that would be inside your example loop?

    1 person found this answer helpful.

  3. Karen Payne MVP 35,376 Reputation points
    2021-10-10T21:29:15.633+00:00

    I'd recommend creating unit test and implement BenchmarkDotNet (even Microsoft uses this package). Using this package will indicate what code may need optimizing.

    BenchmarkDotNet helps you to transform methods into benchmarks, track their performance, and share reproducible measurement experiments. It's no harder than writing unit tests! Under the hood, it performs a lot of magic that guarantees reliable and precise results thanks to the perfolizer statistical engine. BenchmarkDotNet protects you from popular benchmarking mistakes and warns you if something is wrong with your benchmark design or obtained measurements. The results are presented in a user-friendly form that highlights all the important facts about your experiment. The library is adopted by 6800+ projects including .NET Runtime and supported by the .NET Foundation.

    Also, with each new major release of the .NET Framework there are performance optimization e.g. Performance Improvements in .NET 6. If using VS2019, to use Net Core 6 download the SDK. Create a new project, change the project file to the following.

    <Project Sdk="Microsoft.NET.Sdk">
    
        <PropertyGroup>
            <EnablePreviewFeatures>true</EnablePreviewFeatures>
            <LangVersion>preview</LangVersion>
            <OutputType>Exe</OutputType>
            <TargetFramework>net6.0</TargetFramework>
        </PropertyGroup>
    
        <ItemGroup>
            <PackageReference Include="System.Runtime.Experimental" Version="6.0.0-preview.7.21377.19" />
        </ItemGroup>
    
    </Project>
    
    1 person found this answer helpful.
    0 comments No comments

  4. Timon Yang-MSFT 9,581 Reputation points
    2021-10-11T03:15:18.45+00:00

    Parallel loops can improve loop efficiency, but not in all cases, depending on the complexity of the task.

    Generally speaking, the more complex the code in the loop, the more we tend to use parallelism. But there is no definite limit to the degree of complexity, and we cannot say exactly whether it should be used or not. This requires you to test first and then decide for yourself.

    If your program has put the CPU under a high load, then upgrading the CPU should improve efficiency, and since your task is related to IO, then upgrading the hard disk with faster read and write speed should also improve the efficiency.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

  5. Bruce (SqlWork.com) 60,866 Reputation points
    2021-10-10T15:17:03.48+00:00

    Yes, it will depend on the language and maybe the o/s. But what’s in the loop will differ more. Languages that use a runtime rather than compile to machine code will be slower.

    0 comments No comments