Dart 与 PHP,PHP 比 Dart (aot) 快得多

发布于 2025-01-18 14:31:00 字数 1122 浏览 1 评论 0原文

我最近开始学习服务器端的 Dart,因为我喜欢它的语法和 aot 概念。我用两种语言编写了一个简单的脚本,似乎 PHP 比 Dart 生成的 AOT 可执行文件快得多。

Dart 代码

import "dart:io";

main() {
  var i = 0;
  while (true) {
    stdout.write("i = $i\n");
    if (i == 1000000) {
      break
    }
    i++;
  }
}

编译为: dart 编译 exe file-name.dart

PHP

<?php
$i = 0;
while (true) {
  print("i = $i\n");
  if ($i === 1000000) {
     break;
  }
  $i++;

php file-name.php

性能

Performance-Image Imgur

我的问题是为什么 PHP 比 Dart 快约 50%? 即使我尝试先在没有 measure-command 的情况下运行 dart,直到看到 dart 应用程序打印到屏幕上,然后我运行 php,php 仍然运行得更快,直到它通过 dart 打印的 i< /code> value

我不确定我是否做错了什么,因为我知道 AOT 编译为本机代码,并且我知道这与 IO 而不是计算相关,这可能会对基准测试产生不同的影响。

还有一个问题我在 dart.dev 网站上找不到答案。将 dart 代码编译到 AOT 时,是否会将代码编译为本机 cpu 指令,例如,如果我说

i = i + 1;

将在 x86 或类似的平台上

add i, 1

I started recently learning Dart for server side as I like its syntax and aot concept. I wrote a simple script in both language and seems PHP way faster than Dart generated AOT executable.

Dart Code

import "dart:io";

main() {
  var i = 0;
  while (true) {
    stdout.write("i = $i\n");
    if (i == 1000000) {
      break
    }
    i++;
  }
}

Compiled with:
dart compile exe file-name.dart

PHP

<?php
$i = 0;
while (true) {
  print("i = $i\n");
  if ($i === 1000000) {
     break;
  }
  $i++;

php file-name.php

Performance

Performance-Image Imgur

My question is why PHP is faster by ~50% than Dart?
Even I tried to run the dart first without measure-command until I see dart app prints to the screen, then I run php, php still go way faster until it pass the dart printed i value

I am not sure if I did something wrong, as I know that AOT compiles to native code and I know this related to IO not computation which might has different impact on the benchmark.

And another question that I could not get the answer in dart.dev website. When compiling dart code to AOT is it going to compile the code to native cpu instructions for instance if I say

i = i + 1;

will be on x86 or similar

add i, 1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文