You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rust实现MUID哈希匹配性能远逊于Python,求原因解析

为什么我的Rust MUID挖矿代码比Python版本慢这么多?

我在阅读关于「Memorable Unique Identifiers(MUID)」的文章后,看到作者提及想将示例Python代码改写为C以提升性能,于是尝试将其改写为Rust,却惊讶地发现Rust版本的性能明显慢于Python版本。

测试环境

  • 2021款M1 Pro MacBook Pro
  • Python 3.10.9
  • Rust nightly-aarch64-apple-darwin(rustc 1.69.0-nightly (07c993eba 2023-02-23))

Python版本代码(略有修改)

import json
import hashlib
import os

TARGET_DIFF = 8

prefixes = set()

with open("animals.json") as f:
    f = json.load(f)
    for key in f.keys():
        if len(key) == TARGET_DIFF:
            prefixes.add(key)

counter = 0

while counter < 10:
    buf = os.urandom(16).hex()
    h = hashlib.sha256(buf.encode("utf-8")).digest().hex()
    if h[:TARGET_DIFF] in prefixes:
        print(buf, h)
        counter += 1

该代码找到10个哈希值耗时约7秒:

> time python3 miner.py
python3 miner.py  7.60s user 3.00s system 99% cpu 10.626 total

我编写的Rust版本代码

use std::{
    fs::File,
    io::{prelude::*, BufReader},
    path::Path,
    fmt::Error,
    env,
    process
};

use core::fmt::Write;

use sha256;

use rand_xoshiro::rand_core::SeedableRng;
use rand_xoshiro::Xoshiro256PlusPlus;
use rand::Rng;

const TARGET_DIFF: usize = 8;

fn lines_from_file(filename: impl AsRef<Path>) -> Vec<String> {
    let file = File::open(filename).expect("no such file");
    let buf = BufReader::new(file);
    
    buf.lines()
        .map(|l| l.expect("could not parse line"))
        .filter(|s| s.len() == TARGET_DIFF)
        .collect()
}

fn to_hex(input: &mut [u8]) -> Result<String, Error> {
    let mut s = String::with_capacity(2 * input.len());
    
    for byte in input {
        write!(s, "{:02x}", byte)?;
    }
    
    Ok(s)
}

fn fill_with_random(input: &mut [u8], rng: &mut Xoshiro256PlusPlus) -> Result<(), Error> {
    for i in 0..input.len() {
        input[i] = rng.gen::<u8>();
    }
    
    Ok(())
}

fn main() {
    let args: Vec<String> = env::args().collect();
    
    if args.len() < 2 {
        println!("Usage: ./muid <seed>");
        process::exit(1);
    }
    
    let seed: i32 = match args[1].parse::<i32>() {
        Ok(val) => val,
        Err(error) => {
            println!("Invalid argument: {:?}", error);
            process::exit(1);
        },
    };
    
    let mut rng = Xoshiro256PlusPlus::seed_from_u64(seed.try_into().unwrap());
    let lines = lines_from_file("keywords");
   
    println!("{0} {1: >31} {2: >61}", "Private Key", "Public Key", "Keyword");
    println!("-----------------------------------------------------------------------------------------------------------");
    
    let mut counter = 0;
    
    while counter < 10 {
        let mut rand: [u8; 16] = [0; 16];
        fill_with_random(&mut rand, &mut rng).unwrap();
        
        let rand_str = to_hex(&mut rand).unwrap();
        let data_digest = sha256::digest(rand_str.clone());

        let head = &data_digest[..TARGET_DIFF];
        
        if lines.contains(&head.to_string()) {
            println!("{} {} {}", rand_str, data_digest, head);
            counter += 1;
        } 
    }
}

经cargo build --release编译后,该代码找到10个哈希值耗时超44秒:

> ./target/release/uid 0
./target/release/muid 0  44.79s user 0.13s system 99% cpu 44.986 total

我猜测可能是自身Rust使用经验不足导致的错误,或是Rust的随机数生成器(RNG)性能问题,恳请有人解释为何Rust版本性能差距如此之大。

内容的提问来源于stack exchange,提问作者Jakub S.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 02:19:59