Skip to content

iXyles/wasm-performance-issue-sample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

wasm-performance-issue-sample

Sample to demonstrate Linq & For loop search performance issue in wasm vs native console app This example is based on .NET 6

Performance difference (executed code on 6.0.1)

Without AOT

execution-time

Performance difference (executed code on .NET 7 RC 2)

Without AOT

execution-time

With AOT

with-AOT

Link to code

https://github.com/iXyles/wasm-performance-issue-sample/blob/1d8255c85ef01235ef8d36f29ebdbe8a2dc28589/shared-code/SharedCode.cs

Code

public class SharedCode
{
    private const int I = 5000; // iterations we perform the look up
    private const int N = 3000; // the amount of items we have.
    private readonly List<Guid> _items = new();
    private readonly Guid _last;

    public SharedCode()
    {
        for (var i = 0; i < N; i++)
            _items.Add(Guid.NewGuid());
        _last = _items.Last();
    }

    public void PerformanceTest()
    {
        LinqFirstTest();
        ForLoopFirstTest();
    }

    private void ForLoopFirstTest()
    {
        Console.WriteLine($"For loop first test (worst case scenario {I} times on {N} items) start");
        var watch = new Stopwatch();
        watch.Start();

        for (var i = 0; i < I; i++)
            _items.First(i => i == _last);

        watch.Stop();
        Console.WriteLine($"For loop first test (worst case scenario {I} times on {N} items) finished, time: {watch.ElapsedMilliseconds}ms");
    }

    private void LinqFirstTest()
    {
        Console.WriteLine($"For loop first test (worst case scenario {I} times on {N} items) start");
        var watch = new Stopwatch();
        watch.Start();

        for (var i = 0; i < I; i++)
            foreach (var item in _items)
                if (item == _last)
                    break;

        watch.Stop();
        Console.WriteLine($"For loop first test (worst case scenario {I} times on {N} items) finished, time: {watch.ElapsedMilliseconds}ms");
    }
}

About

Sample to demonstrate Linq & For loop search performance issue in wasm vs native console app

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published