忽略休息的有效划分方法
我发现有两种方法可以从c ++中的除法中获取整数
问题是哪种方法更有效(更快)
第一种方法:
Quotient = value1 / value2; // normal division haveing splitted number
floor(Quotient); // rounding the number down to the first integer
第二种方法:
Rest = value1 % value2; // getting the Rest with modulus % operator
Quotient = (value1-Rest) / value2; // substracting the Rest so the division will match
还请演示如何找出哪种方法更快
there are 2 ways i found to get a whole number from a division in c++
question is which way is more efficient (more speedy)
first way:
Quotient = value1 / value2; // normal division haveing splitted number
floor(Quotient); // rounding the number down to the first integer
second way:
Rest = value1 % value2; // getting the Rest with modulus % operator
Quotient = (value1-Rest) / value2; // substracting the Rest so the division will match
also please demonstrate how to find out which method is faster
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您正在处理整数,那么通常的方法就是
这样。结果已经是一个整数了。无需使用
floor(Quotient);
语句。反正也没有什么效果。如果需要,您可能需要使用Quotient = Floor(Quotient);
。如果您有浮点数,那么第二种方法根本不起作用,因为
%
仅为整数定义。但是从实数相除得到整数是什么意思呢? 8.5 除以 3.2 会得到什么整数?问这个问题有意义吗?附带说明一下,您所说的“休息”通常称为
“提醒”。剩余。If you're dealing with integers, then the usual way is
That's it. The result is already an integer. No need to use the
floor(Quotient);
statement. It has no effect anyway. You would want to useQuotient = floor(Quotient);
if it was needed.If you have floating point numbers, then the second method won't work at all, as
%
is only defined for integers. But what does it mean to get a whole number from a division of real numbers? What integer do you get when you divide 8.5 by 3.2? Does it ever make sense to ask this question?As a side note, the thing you call 'Rest' is normally called
'reminder'.remainder.使用此程序:
您可以计时执行
或者查看汇编输出:
如您所见,仅执行除法更快。
编辑以修复代码、格式和错误差异中的错误。
更多编辑(解释程序集差异):在第二种情况下,当首先进行模数时,程序集显示需要两个
idivl
操作:一个用于获取%
和一个用于实际划分的。上面的差异显示了减法和第二次除法,因为第一个在两个代码中完全相同。编辑:更多相关的计时信息:
希望有帮助。
编辑:使用
-O0
和不使用-O0
的程序集之间的差异。由于
gcc
的默认优化级别是O0
(参见本文解释了gcc
中的优化级别)结果是预期的。编辑:如果您按照注释之一使用
-O3
进行编译,您将获得相同的程序集,在该优化级别,两种替代方案是相同的。Use this program:
You can time execution
Or, you look at the assembly output:
As you see, doing only the division is faster.
Edited to fix error in code, formatting and wrong diff.
More edit (explaining the assembly diff): In the second case, when doing the modulus first, the assembly shows that two
idivl
operations are needed: one to get the result of%
and one for the actual division. The above diff shows the subtraction and the second division, as the first one is exactly the same in both codes.Edit: more relevant timing information:
Hope it helps.
Edit: diff between assembly with
-O0
and without.Since the defualt optimization level of
gcc
isO0
(see this article explaining optimization levels ingcc
) the result was expected.Edit: if you compile with
-O3
as one of the comments suggested you'll get the same assembly, at that level of optimization, both alternatives are the same.