2

我在 Angular 和 Blazor(客户端)中执行相同的一小段代码,但我不了解这两个系统之间的性能差距。角度看起来要快得多。

我正在使用最新版本的 Windows 10 (1903)。我正在使用 Chrome 版本 76.0.3809.100(构建官方)(64 位)。

代码非常简单:对于 Blazor

@code {
    int currentCount = 0;

    void process()
    {
        DateTime from = DateTime.Now;
        Console.WriteLine (from);

        long nombreOccurences = 100000000;
        Console.WriteLine ("Nombre de factoriels " + nombreOccurences );

        for (int currentCount = 0; currentCount < nombreOccurences; currentCount++) {
            long result = factorial(currentCount%21);
        }

        Console.WriteLine(currentCount);

        DateTime to = DateTime.Now;
        Console.WriteLine (to);

        TimeSpan diff = to - from;
        Console.WriteLine("diff " + diff);
    }

    long factorial (long n) {
        return n>1 ? n*factorial(n-1) : 1;
    }
}

对于角

process() {
    let currentCount = 0;
    let result = 0;

    console.log('Process');

    const from = new Date();
    console.log('from', from);

    const nombreOccurences = 100000000;
    console.log('Nombre de factoriels ', nombreOccurences);

    for (currentCount = 0; currentCount < nombreOccurences; currentCount++) {
      result = this.factoriel(currentCount % 21);
    }

    console.log ('currentCount', currentCount);

    const to = new Date();
    console.log(to);

    var diff = to.getTime() - from.getTime();
    console.log('diff ', diff);
}

public factoriel(n: number) {
    return n > 1 ? n * this.factoriel(n - 1) : 1;
}

Angular 控制台:

from Thu Aug 08 2019 16:21:35 GMT+0200 (heure d’été d’Europe centrale)
app.component.ts:22 Nombre de factoriels  10000000
app.component.ts:28 currentCount 10000000
app.component.ts:31 Thu Aug 08 2019 16:21:36 GMT+0200 (heure d’été d’Europe centrale)
app.component.ts:34 diff  795

Blazor 控制台:

ASM: 08/08/2019 14:23:54
blazor.webassembly.js:1 WASM: Nombre de factoriels 100000000
blazor.webassembly.js:1 WASM: 0
blazor.webassembly.js:1 WASM: 08/08/2019 14:24:52
blazor.webassembly.js:1 WASM: diff 00:00:58.2570000
4

1 回答 1

0

Blazor Webassembly 比 JavaScript 慢很多。请参阅https://github.com/mono/mono/issues/9825

但是,现在,可以使用 Blazor WebAssembly 提前 (AOT) 编译,与 JavaScript 没有太大差距,参见https://devblogs.microsoft.com/aspnet/asp-net-core-updates-in- net-6-preview-4/

于 2021-07-19T06:12:21.880 回答