乐趣区

关于rust:Rust和V冒泡排序对比测试benchmark

介绍

V 语言在 HN 上常常被探讨,号称有几个长处:

  1. 平安(默认不可变变量等相似 Rust 个性)
  2. 简略易用(语法一看就懂)
  3. 内置包管理工具(VPM)
  4. 跨平台 UI

目标

  1. 初步理解 V 语言;
  2. 学习 valgrind 工具获取程序运行信息;
  3. 冒泡排序 Rust 比照 V 语言;
  4. 作为技术决策的调研数据;

V 的其它劣势

能够将 C 语言工程间接翻译为 V

hot reload

相似于增量编译,每次批改代码不须要编译整个工程

基于 OpenGL/Metel/DirectX11 的图形库

  1. 加载 3D 模型和材质
  2. Camera
  3. 骨骼动画

穿插编译容易

加一个参数就能够在 Windows 平台上编译为 Linux,这个比拟弱小,目前支流穿插编译的我的项目例如 ReactNative,Electron 的做法都比这个要麻烦。
v -os windows 或者 v -os linux 即可。
没搞懂它背地如何实现的,可能搞了一种字节码和 runtime。

测试条件

硬件

CPU: Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz 3.19 GHz

RAM: 16.0 GB

OS

Microsoft Windows 11

WSL2 Ubuntu 22.04.2 LTS

我的项目

  1. 文件读取
  2. JSON parse
  3. 冒泡排序

所以正好有趣味测试一下 V 语言是否像它声称的那么好。测试条件我抉择从文件中读取 JSON 数据,剖析后对数组进行冒泡排序,这个测试第一能够测试 IO 性能,第二减少了 JSON parse 的过程,最初用冒泡排序查看内存应用状况。

代码如下:

import os
import time
import json

fn main() {sw := time.new_stopwatch()
    str := os.read_file('../data.json')!
    mut arr := json.decode([][]f64, str)!
    // println(arr)
    for i in 0..arr.len {bubble_sort(mut arr[i])
        // println(arr[i])
    }
    println('耗费工夫 : ${sw.elapsed().milliseconds()}ms')
}


// Bubble Sort
[direct_array_access]
fn bubble_sort(mut array []f64) {
    for i in 0..array.len {
        for j in 0..array.len - 1 - i {if array[j] > array[j + 1] {array[j], array[j + 1] = array[j + 1], array[j]
            }
        }
    }
    // println('${array[i]}')
}

Rust 局部代码

use std::fs;
use std::io::{Write, BufReader, BufRead, Error};
#[macro_use]
extern crate serde_json;

fn bubble_sort<T: PartialOrd>(arr: &mut [T]) {for i in 0..arr.len() {for j in 0..arr.len() - 1 - i {// println!("{}", arr[j]);
            if arr[j] > arr[j + 1] {arr.swap(j, j + 1);
            }
        }
    }
}

fn main() -> Result<(), Error> {
    let path = "../data.json";
    println!("In file {}", path);
    let contents = fs::read_to_string(path)
        .expect("Should have been able to read the file");
    let v: Vec<Vec<f64>> = serde_json::from_str(&contents).unwrap();
    // println!("{:?}",v);
    for mut x in v {bubble_sort(&mut x);
    }
    Ok(())
}

执行命令

Memory:
valgrind --tool=memcheck ./target/release/rust
valgrind --tool=memcheck ./sort

CPU:
valgrind --tool=callgrind ./target/release/rust
valgrind --tool=callgrind ./sort
# CPU 后果生成图
gprof2dot --format=callgrind --output=out.dot ./callgrind.out.521
dot -Tpng out.dot -o graph.png

测试后果

V

==507==
==507== HEAP SUMMARY:
==507==     in use at exit: 2,992 bytes in 11 blocks
==507==   total heap usage: 505,129 allocs, 505,118 frees, 32,341,222 bytes allocated
==507==
==507== LEAK SUMMARY:
==507==    definitely lost: 0 bytes in 0 blocks
==507==    indirectly lost: 0 bytes in 0 blocks
==507==      possibly lost: 2,992 bytes in 11 blocks
==507==    still reachable: 0 bytes in 0 blocks
==507==         suppressed: 0 bytes in 0 blocks
==507== Rerun with --leak-check=full to see details of leaked memory
==507==
==507== Use --track-origins=yes to see where uninitialised values come from
==507== For lists of detected and suppressed errors, rerun with: -s
==507== ERROR SUMMARY: 1990 errors from 102 contexts (suppressed: 0 from 0)

Rust

==504== Memcheck, a memory error detector
==504== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==504== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==504== Command: ./target/release/rust
==504==
In file ../data.json
==504==
==504== HEAP SUMMARY:
==504==     in use at exit: 0 bytes in 0 blocks
==504==   total heap usage: 1,159 allocs, 1,159 frees, 21,370,367 bytes allocated
==504==
==504== All heap blocks were freed -- no leaks are possible
==504==
==504== For lists of detected and suppressed errors, rerun with: -s
==504== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

精简版

内存

Rust:1159 次 alloc,1159 次 free,共 20.38MB

V: 505129 次 alloc, 505118 次 free,共 30.84MB

CPU

V
Rust

后果

  1. V 语言内存占用显然比 Rust 多。
  2. JSON parse 后,V 应用了 11.65% 的 CPU 工夫用于 __memcpy_avx_unaligned_erm,内存对齐?没搞懂,反正 V 的工夫比 Rust 长很多。

所有代码在:
tcper/rustvsv (github.com)

退出移动版