有效地将两个整数x和y转换为浮点数x.y [英] Efficiently convert two Integers x and y into the float x.y

查看:183
本文介绍了有效地将两个整数x和y转换为浮点数x.y的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出两个整数X和Y,在C ++中将它们转换为X.Y浮点值的最有效方法是什么?

Given two integers X and Y, whats the most efficient way of converting them into X.Y float value in C++?

例如

 X = 3, Y = 1415 -> 3.1415

 X = 2, Y = 12   -> 2.12

推荐答案

以下是我的机器上一些鸡尾酒餐巾基准测试结果,这些结果截止到将两个int转换为float的所有解决方案写作.

Here are some cocktail-napkin benchmark results, on my machine, for all solutions converting two ints to a float, as of the time of writing.

注意:我现在添加了自己的解决方案,该方法似乎效果很好,因此存在偏见!请仔细检查我的结果.

Caveat: I've now added a solution of my own, which seems to do well, and am therefore biased! Please double-check my results.

Test                              Iterations    ns / iteration
--------------------------------------------------------------
@aliberro's conversion v2         79,113,375                13
@3Dave's conversion               84,091,005                12
@einpoklum's conversion        1,966,008,981                 0
@Ripi2's conversion               47,374,058                21
@TarekDakhran's conversion     1,960,763,847                 0

  • CPU:四核Intel Core i5-7600K速度/最小/最大:4000/800/4200 MHz
  • Devuan GNU/Linux 3
  • 内核:5.2.0-3-amd64 x86_64
  • GCC 9.2.1,带有标志:-O3 -march=native -mtune=native
    • CPU: Quad Core Intel Core i5-7600K speed/min/max: 4000/800/4200 MHz
    • Devuan GNU/Linux 3
    • Kernel: 5.2.0-3-amd64 x86_64
    • GCC 9.2.1, with flags: -O3 -march=native -mtune=native
    • 基准代码(Github Gist).

      Benchmark code (Github Gist).

      这篇关于有效地将两个整数x和y转换为浮点数x.y的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆