记录一次rust浮点数计算没有java速度快的例子

不同的电脑执行如下代码可能会有不同的结果。但是我在同一台机器上的结果,java会比rust快5倍。

java代码:

java 复制代码
public class Test {
    private static final int MAX = 10000000;
    public static void main(String[] args) {
        test(1, 3.141592653589793, 2.718281828459045, "Addition");
        test(2, 3.141592653589793, 2.718281828459045, "Subtraction");
        test(3, 3.141592653589793, 2.718281828459045, "Multiplication");
        test(4, 3.141592653589793, 2.718281828459045, "Division");
    }
    public static void test(int step, double a, double b, String msg) {
        long startTime = System.currentTimeMillis();
        double sum = 0;
        switch (step) {
            case 1 -> {
                for (int i = 0; i < MAX; i++) {
                    sum += a + b;
                }
            }
            case 2 -> {
                for (int i = 0; i < MAX; i++) {
                    sum += a - b;
                }
            }
            case 3 -> {
                for (int i = 0; i < MAX; i++) {
                    sum += a * b;
                }
            }
            case 4 -> {
                for (int i = 0; i < MAX; i++) {
                    sum += a / b;
                }
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println(msg + " Time taken: " + (endTime - startTime) + "ms result: " + sum);
    }
}

java代码的运行结果

bash 复制代码
Addition Time taken: 15ms result: 5.8598744811975874E7
Subtraction Time taken: 14ms result: 4233108.251312001
Multiplication Time taken: 17ms result: 8.539734223664734E7
Division Time taken: 15ms result: 1.155727349739092E7
rust 复制代码
fn main() {
    _test1(1, 3.141592653589793, 2.718281828459045, "Addition");
    _test1(2, 3.141592653589793, 2.718281828459045, "Subtraction");
    _test1(3, 3.141592653589793, 2.718281828459045, "Multiplication");
    _test1(4, 3.141592653589793, 2.718281828459045, "Division");
}
const MAX: usize = 10000000;
fn _test1(step: u8, a: f64, b: f64, msg: &str) {
    let start = std::time::Instant::now();
    let mut sum = 0.0;
    match step {
        1 => {
            for _ in 0..MAX {
                sum += a + b;
            }
        }
        2 => {
            for _ in 0..MAX {
                sum += a - b;
            }
        }
        3 => {
            for _ in 0..MAX {
                sum += a * b;
            }
        }
        4 => {
            for _ in 0..MAX {
                sum += a / b;
            }
        }
        _ => {
            println!("Invalid step");
        }
    }
    println!("{} Time take: {:?}ms result: {}", msg, start.elapsed().as_millis(), sum);
}
bash 复制代码
Addition Time take: 82ms result: 58598744.811975874
Subtraction Time take: 75ms result: 4233108.251312001
Multiplication Time take: 73ms result: 85397342.23664734
Division Time take: 70ms result: 11557273.49739092
相关推荐
brrdg_sefg8 分钟前
gitlab代码推送
java
m0_7482309417 分钟前
Rust赋能前端: 纯血前端将 Table 导出 Excel
前端·rust·excel
hanbarger31 分钟前
mybatis框架——缓存,分页
java·spring·mybatis
cdut_suye38 分钟前
Linux工具使用指南:从apt管理、gcc编译到makefile构建与gdb调试
java·linux·运维·服务器·c++·人工智能·python
苹果醋31 小时前
2020重新出发,MySql基础,MySql表数据操作
java·运维·spring boot·mysql·nginx
小蜗牛慢慢爬行1 小时前
如何在 Spring Boot 微服务中设置和管理多个数据库
java·数据库·spring boot·后端·微服务·架构·hibernate
azhou的代码园1 小时前
基于JAVA+SpringBoot+Vue的制造装备物联及生产管理ERP系统
java·spring boot·制造
wm10432 小时前
java web springboot
java·spring boot·后端
smile-yan2 小时前
Provides transitive vulnerable dependency maven 提示依赖存在漏洞问题的解决方法
java·maven
老马啸西风2 小时前
NLP 中文拼写检测纠正论文-01-介绍了SIGHAN 2015 包括任务描述,数据准备, 绩效指标和评估结果
java