c# javascript
Which technology should be chosen for performance critical applications? How does Node, .NET Core, or Electron compare to native applications?
性能关键型应用应选择哪种技术? Node,.NET Core或Electron与本地应用程序相比如何?
Different languages and runtimes feature distinct advantages in their respective domains; so, apples to oranges really…
不同的语言和运行时在各自的领域中具有明显的优势。 因此,苹果真的变成了桔子...
As well, it’s interesting how different code segments translate in regards to performance.
同样,有趣的是不同的代码段如何转换性能。
All tests run on a MacBook Pro 15" 2018, 2.2 GHz 6-Core Intel Core i7–8750H with macOS Catalina.
所有测试均在配备macOS Catalina的MacBook Pro 15“ 2018、2.2 GHz六核Intel Core i7–8750H上运行。
加法赋值运算符 (Addition Assignment Operator)
To start things off, simple arithmetic comparing JavaScript, C, and C# with 200-million addition assignment operators using the following segments of code:
首先,使用以下代码段,通过简单的算法将JavaScript,C和C#与2亿个加法赋值运算符进行比较:
// C
int temp = 1;
for (int i = 0; i < 200000000; i++)
temp += temp;// C#
int temp = 1;
for (var i = 0; i < 200000000; i++)
temp += temp;// JavaScript
let temp = 1;
for (let i = 0; i < 200000000; i++)
temp += temp;
For C, I’ll use CLion executing a C11 release build:
对于C,我将使用CLion执行C11版本构建:
![Image for post](https://miro.medium.com/freeze/max/9999/1*og83uASSt9s7EHgLli9kpA.gif)
For C#, I’ll use Rider executing .NET Core 3.1.301 release build:
对于C#,我将使用Rider执行.NET Core 3.1.301发行版:
![Image for post](https://miro.medium.com/freeze/max/9999/1*BissBRDH77e1M7b41hA18w.gif)
For JavaScript, I’ll use IntelliJ IDEA executing in Node v14.8.0:
对于JavaScript,我将使用在Node v14.8.0中执行的IntelliJ IDEA:
![Image for post](https://miro.medium.com/freeze/max/9999/1*2GryYV6qBn_hfqrLMHW6ew.gif)
Results:
结果:
![Image for post](https://miro.medium.com/max/9999/1*9lLLwU1dB5hAI8LDyfMX9A.png)
- C: 6.219 milliseconds, ~160 per second C:6.219毫秒,每秒〜160
- C#: 109 milliseconds, ~9 per second C#:109毫秒,每秒〜9
- JS: 202 milliseconds, ~5 per second JS:202毫秒,每秒〜5
对数 (Logarithm)
For a heavier mathematical operation, comparing JavaScript, C, and C# with 100-million logarithms using the following segments of code:
对于较重的数学运算,请使用以下代码段将JavaScript,C和C#与1亿个对数进行比较:
// C
double n;
for (int i = 0; i < 100000000; i++)
n = log(i);// C#
double n;
for (var i = 0; i < 100000000; i++)
n = Math.Log(i);// JavaScript
let ret;
for (let i = 0; i < 100000000; i++)
n = Math.log(i);
Results:
结果:
![Image for post](https://miro.medium.com/max/9999/1*kECfUjrXj5Jku5yTKTWMSw.png)
- C: 33.563 milliseconds, ~30 per second C:33.563毫秒,每秒〜30
- C#: 465 milliseconds, ~2 per second C#:465毫秒,每秒〜2
- JS: 947 milliseconds, ~1 per second JS:947毫秒,每秒〜1
阵列人口 (Array population)
Comparing JavaScript, C, and C# with 10-million array assignments using the following segments of code:
使用以下代码段将JavaScript,C和C#与1000万个数组分配进行比较:
// C
int a[10000000];
for (int i = 0; i < 10000000; i++)
a[i] = i;// C#
int[] a = new int[10000000];
for (var i = 0; i < 10000000; i++)
a[i] = i;// JavaScript
let a = new Array(10000000);
for (let i = 0; i < 10000000; i++)
a[i] = i;
Results:
结果:
![Image for post](https://miro.medium.com/max/9999/1*IZYybsj3MB22fYj_2wJ4Qw.png)
- C: 3.081 milliseconds, ~325 per second C:3.081毫秒,〜325 /秒
- C#: 21 milliseconds, ~47 per second C#:21毫秒,每秒约47
- JS: 9 milliseconds, ~111 per second JS:9毫秒,每秒〜111
Note that initializing the JavaScript array at the correct size is critical; otherwise, relying on the array grow comes at a substantial cost.
请注意,以正确的大小初始化JavaScript数组至关重要。 否则,依靠阵列增长将付出巨大的代价。
If the JavaScript code was rewritten without initial array size:
如果JavaScript代码是在没有初始数组大小的情况下重写的:
// JavaScript
let a = [];
for (let i = 0; i < 10000000; i++)
a[i] = i;
Results:
结果:
![Image for post](https://miro.medium.com/max/9999/1*SO2k1jSZr_kPX7Ij0fe0mQ.png)
- JS (unoptimized): 270 milliseconds, ~4 per second JS(未优化):270毫秒,每秒〜4
随机数生成 (Random Number Generation)
Comparing JavaScript, C, and C# with 10-million random number generations using the following segments of code:
使用以下代码段,比较具有1000万个随机数生成JavaScript,C和C#:
// C
srand(time(0));
for (int i = 0; i < 10000000; i++)
rand();// C#
var rand = new Random();
for (var i = 0; i < 10000000; i++)
rand.Next();// JavaScript
for (let i = 0; i < 10000000; i++)
Math.random();
Results:
结果:
![Image for post](https://miro.medium.com/max/9999/1*HZ6wm-Ia_KJbI3UvbL4Ttg.png)
- C: 54.738 milliseconds, ~18 per second C:54.738毫秒,每秒〜18
- C#: 87 milliseconds, ~12 per second C#:87毫秒,每秒〜12
- JS: 69 milliseconds, ~15 per second JS:69毫秒,每秒〜15
字符串串联 (String Concatenation)
Comparing JavaScript, C, and C# with 1-million string concatenations using the following segments of code:
使用以下代码段将JavaScript,C和C#与一百万个字符串串联进行比较:
// C
char* result = NULL;
size_t resultSize = 0;
FILE* stream = open_memstream(&result, &resultSize);
for (int i = 0; i < 1000000; i++)
fprintf(stream, "ab", i, i);
fclose(stream);
free(result);// C#
StringBuilder str = new StringBuilder();
for (var i = 0; i < 1000000; i++)
str.Append("ab");// JavaScript
let str = '';
for (let i = 0; i < 1000000; i++)
str += 'ab';
Results:
结果:
![Image for post](https://miro.medium.com/max/9999/1*qOrjs8Ig6FoXzZPqqhdNYg.png)
- C: 71.243 milliseconds, ~14 per second C:71.243毫秒,每秒〜14
- C#: 6 milliseconds, ~166 per second C#:6毫秒,每秒〜166
- JS: 48 milliseconds, ~21 per second JS:48毫秒,每秒〜21
C# has many string concatenation operations, such as the +=
operator, Concat
, Join
, Format
, or interpolation, but above leverages the StringBuilder
for highest performance.
C#具有许多字符串连接操作,例如+=
运算符, Concat
, Join
, Format
或插值,但是以上这些都利用StringBuilder
来实现最高性能。
If we were to substitute that with a +=
operator as in the JavaScript example:
如果像JavaScript示例中那样用+=
运算符代替它:
// C#
string str = "";
for (var i = 0; i < 1000000; i++)
str += "ab";
Results:
结果:
![Image for post](https://miro.medium.com/max/9999/1*CCQYyCv-GH4XtPtuBDUDfg.png)
- C# (unoptimized): 7.9 minutes C#(未优化):7.9分钟
C also requires optimization, as using strcat
is also slow:
C也需要优化,因为使用strcat
的速度也很慢:
// C
char *str1;
str1 = malloc(1000001*2*sizeof(char));
char *str2 = "ab";
for (int i = 0; i < 1000000; i++)
strcat(str1, str2);
![Image for post](https://miro.medium.com/max/9999/1*41XDwQO7KO2TteNwuyXBGg.png)
- C (unoptimized): 29 seconds C(未优化):29秒
结论 (Conclusion)
Nothing from this thin cross-section mirrors real world examples, and there are further optimizations that could be considered here.
如此薄的横截面没有任何东西可以反映现实世界中的示例,这里可以考虑进一步的优化。
Even the concept of performance speed could be debated, taking into account the initial JIT compile time loss, parallel concurrency capabilities, or maturity of optimizations automatically applied by compilers.
考虑到最初的JIT编译时间损失,并行并发功能或编译器自动应用的优化成熟度,甚至可以讨论性能速度的概念。
These five examples focus on mathematics and memory management, while exposing how thought in implementation can dramatically affect outcomes.
这五个示例着重于数学和内存管理,同时揭示了实现中的思考如何能极大地影响结果。
It’s also exciting to see modern engines like JavaScript edging closer to native performance.
看到像JavaScript这样的现代引擎更接近本机性能,这也令人兴奋。
翻译自: https://medium.com/@jasonsturges/how-fast-is-javascript-to-c-or-c-29d104f4f255
c# javascript