Dart 运行速度测评与比较
引言
Dart 是一门优秀的跨平台语言,尽管生态方面略有欠缺,但无疑作为一门编程语言来说,Dart 是很优美,很健壮的,同时也引入了一些先进的编程范式,值得去学习。
测试内容
现在,我们就来测评一下Dart 语言的运行速率,测试平台为WSL(win子系统,Ubuntu18.04),有以下几个对比项:
| 编程语言 | 版本 |
|---|---|
| Dart VM | Dart VM 2.7.1 |
| Dart-to-JavaScript | dart2js 2.7.1 |
| Dart-to-Native | 同上 |
| Java | openjdk 11.0.6 |
| C# | .NET Core SDK 3.1.102 |
| C | gcc 7.5.0 |
| JavaScript | node 8.10.0 |
| Python | Python 3.6.9 |
测试用代码如下:
for (i = 0; i < 10000; i++)
for (j = 0; j < 10000; j++)
a[i][j] = i * j + 1;
最后统计此代码段运行时间,各个语言的具体代码见本文最后。
测试结果
| 编程语言 | 运行指令 | 运行时间/ms |
|---|---|---|
| Dart VM | dart t.dart | 375 |
| Dart-to-JavaScript | dart2js t.dart && node out.js | 638 |
| Dart-to-Native | dart2native t.dart && ./t.exe | 631 |
| Java | javac t.java && java t | 126 |
| C# | dotnet restore && dotnet run | 964 |
| C | gcc t.c && ./a.out | 313 |
| JavaScript | node t.js | 245 |
| Python | python3 t.py | 22403 |
额...与我设想的结果好像不太一样...那我们来排个序:
- Java
- JavaScript
- C
- Dart VM
- Dart-to-Native
- Dart-to-JavaScript
- C#
- Python
首先声明一下,就语言本身的优美程度来讲,我的确是有点不喜欢Python,但我也没想到它会这么慢...还不是一个数量级,开始还以为电脑卡住了,又试了几下大概都还是这么多。当然,Python的强项也不在此,而且如果使用numpy优化处理该代码段的话,速度一定不会这么不堪。
同时C#的.net core也被openjdk远远甩开了,果然,C#最好还是要用在windows上。本来我是要更喜欢C#的,哎但是这个速度...
最让我惊讶的是,本来感觉毫无疑问应该排第一的C语言竟然被两种解释性语言给甩开了。也...许,是WSL的问题?
总之,Dart的运行速率基本在正常范围内,并且运行也很方便,再就是跨平台性,flutter也将赋予dart更强大的生命力。
测试代码
Java
public class t {
public static long[][] a = new long[10000][];
public static void main(String[] args) {
long t0, t1;
for (int i = 0; i < 10000; i++)
a[i] = new long[10000];
t0 = System.currentTimeMillis();
for (int i = 0; i < 10000; i++)
for (int j = 0; j < 10000; j++)
a[i][j] = i * j + 1;
t1 = System.currentTimeMillis();
System.out.println("Running Time: " + (float) (t1 - t0) + "ms");
}
}
JavaScript
a = new Array(10000);
for (let i = 0; i < 10000; i++)
a[i] = new Array(10000);
console.time('Running Time');
for (let i = 0; i < 10000; i++)
for (let j = 0; j < 10000; j++)
a[i][j] = i * j + 1;
console.timeEnd('Running Time');
C
#include <stdio.h>
#include <time.h>
int a[10000][10000];
int main()
{
int i, j;
double t1, t2;
t1 = clock();
for (i = 0; i < 10000; i++)
for (j = 0; j < 10000; j++)
a[i][j] = i * j + 1;
t2 = clock();
printf("Running Time: %fms\n", 1000 * (double)(t2 - t1) / CLOCKS_PER_SEC);
return 0;
}
Dart
void main() {
var a = List(10000);
for (int i = 0; i < 10000; i++)
a[i] = List(10000);
var timer = Stopwatch();
timer.start();
for (int i = 0; i < 10000; i++)
for (int j = 0; j < 10000; j++)
a[i][j] = i * j + 1;
timer.stop();
print('Running Time: ' + timer.elapsedMilliseconds.toString() + 'ms');
}
C
using System;
namespace t
{
class Program
{
static int[,] a = new int[10000,10000];
static void Main(string[] args)
{
DateTime beforDT = System.DateTime.Now;
for (int i = 0; i < 10000; i++)
for (int j = 0; j < 10000; j++)
a[i,j] = i * j + 1;
DateTime afterDT = System.DateTime.Now;
TimeSpan ts = afterDT.Subtract(beforDT);
Console.WriteLine("Running Time: {0}ms", ts.TotalMilliseconds);
}
}
}
Python
from time import time
a = [[0 for i in range(10000)] for j in range(10000)]
t0 = time()
for i in range(1000):
for j in range(10000):
a[i][j] = i * j + 1
t1 = time()
print ('Running Time: ' + str(1000*(t1-t0)) + 'ms')
Dart 运行速度测评与比较的更多相关文章
- Sky(dart)语言介绍-android学习之旅(十)
认识dart语言 google于2011年10月10日发布了"dart"语言的"早起预览版",google希望利用这款语言,帮助开发者克服javaScript的 ...
- 小程序多端框架全面测评:chameleon、Taro、uni-app、mpvue、WePY
摘要: 微信小程序开发技巧. 作者:coldsnap 原文:小程序多端框架全面测评 Fundebug经授权转载,版权归原作者所有. 最近前端届多端框架频出,相信很多有代码多端运行需求的开发者都会产生一 ...
- Dart语法基础
hello world // Define a function. printNumber(num aNumber) { print('The number is $aNumber.'); // Pr ...
- Dart基础学习02--变量及内置类型
Dart基础学习02--变量及内置类型 Dart中的变量 首先看一个变量的定义和赋值 var name = 'Bob'; 在Dart中变量名都是引用,这里的name就是一个指向值为Bob的字符串的引用 ...
- Flutter学习笔记(2)--Dart语言简介
Dart简介: Dart诞生于2011年10月10日,Dart是一种"结构化的web编程"语言,Dart虽然是谷歌开发的计算机编程语言,但后来被ECMA认定位标准,这门语言用于We ...
- Dart语法学习
Dart语法学习 目录 参考资料 语言特性 关键字 变量与常量 数据类型 运算符 operators 控制流程语句 异常 Exceptions 函数 Function 类 Class 类-方法 类-抽 ...
- Dart语言概览
## Dart特性 Dart同时支持JIT(Just In Time,即时编译)和AOT(Ahead of Time,运行前编译)两种编译模式. **JIT** 在运行时即时编译,在开发周期中使用,可 ...
- Dart 语言简述
Dart是一种“结构化的web编程”语言,Dart编程语言在所有现代浏览器和环境中提供高性能.Dart是谷歌开发的计算机编程语言,后来被ECMA认定为标准. Dart重要的概念: 1.所有的东西都是对 ...
- Dart的JIT 与 AOT
JIT:Just In Time AOT:Ahead of Time 含义: 目前,程序主要有两种运行方式:静态编译与动态解释. 静态编译的程序在执行前全部被翻译为机器码,通常将这种类型称为AOT ( ...
随机推荐
- tesseract系列(2) -- tesseract的使用
上文说了怎么编译成库,这次说说怎么使用,先验证下编译出来的结果. 下图是debug生成的文件,里面有个tesseract的应用程序. cmd进入目录下,执行命令:tesseract eurotext. ...
- C++各种模板
高精度: 重载运算符版: #include<cstdio> #include<cstring> #include<algorithm> using namespac ...
- 用命令修改Oracle数据库密码
1.改密码 (1).打开doc命令框键入:sqlplus /nolog (2).输入:connect / as sysdba (3).修改密码:alter user userNa ...
- Python_运维中常用的20个库和模块
1.psutil是一个跨平台库(https://github.com/giampaolo/psutil)能够实现获取系统运行的进程和系统利用率(内存,CPU,磁盘,网络等),主要用于系统监控,分析和系 ...
- Maven中settings.xml文件各标签含义
原文地址:http://www.cnblogs.com/jingmoxukong/p/6050172.html?utm_source=gold_browser_extension settings.x ...
- Pay Back(模拟)
链接:https://ac.nowcoder.com/acm/contest/1086/C 题目描述 "Never a borrower nor a lender be." O h ...
- Part-Selenium-1
1.在浏览器中的console输入:帮助定位 > $x('xpath表达式') > $('css表达式') 2.driver > chrome driver: https://npm ...
- JS中获得指定日期前或后几天对应的日期
var d = new Date(); d.setDate(d.getDate() - 2); console.log(d.toString()); // First of month var c = ...
- php 二维码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Java设计模-过滤器模式
过滤器模式 过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接 ...