博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python - abs vs fabs
阅读量:4030 次
发布时间:2019-05-24

本文共 3803 字,大约阅读时间需要 12 分钟。

abs(-5)

Second

import mathmath.fabs(-5)

How do these methods differ?

 

3 Answers

59
accepted

 converts its argument to float if it can (if it can't, it throws an exception). It then takes the absolute value, and returns the result as a float.

In addition to floats,  also works with integers and complex numbers. Its return type depends on the type of its argument.

In [7]: type(abs(-2))Out[7]: intIn [8]: type(abs(-2.0))Out[8]: floatIn [9]: type(abs(3+4j))Out[9]: floatIn [10]: type(math.fabs(-2))Out[10]: floatIn [11]: type(math.fabs(-2.0))Out[11]: floatIn [12]: type(math.fabs(3+4j))---------------------------------------------------------------------------TypeError                                 Traceback (most recent call last)/home/alexei/
in
()----> 1 type(math.fabs(3+4j))TypeError: can't convert complex to float
 
2  
abs works with far more than just integers and floats, and the result type is not always the same as the argument, e.g. abs(3+4j). –   
May 27 '12 at 7:24
1  
add in a comment about fabs taking longer due to its always-float nature and you've got the right answer! –   
May 27 '12 at 7:25
 
@agf: Thanks for reminding me about complex numbers. Out of interest, what other classes of things can __builtin__.abs() be successfully applied to? –   
May 27 '12 at 7:31 
2  
@aix Any user defined class that defines the __abs__ magic method –   
May 27 '12 at 17:59
4

Edit: as @aix suggested, a better (more fair) way to compare the speed difference: 

In [1]: %timeit abs(5)10000000 loops, best of 3: 86.5 ns per loopIn [2]: from math import fabsIn [3]: %timeit fabs(5)10000000 loops, best of 3: 115 ns per loopIn [4]: %timeit abs(-5)10000000 loops, best of 3: 88.3 ns per loopIn [5]: %timeit fabs(-5)10000000 loops, best of 3: 114 ns per loopIn [6]: %timeit abs(5.0)10000000 loops, best of 3: 92.5 ns per loopIn [7]: %timeit fabs(5.0)10000000 loops, best of 3: 93.2 ns per loopIn [8]: %timeit abs(-5.0)10000000 loops, best of 3: 91.8 ns per loopIn [9]: %timeit fabs(-5.0)10000000 loops, best of 3: 91 ns per loop

So it seems abs() only has slight speed advantage over fabs() for integers. For floats, abs()and fabs() demonstrate similar speed.


In addition to what @aix has said, one more thing to consider is the speed difference: 

In [1]: %timeit abs(-5)10000000 loops, best of 3: 102 ns per loopIn [2]: import mathIn [3]: %timeit math.fabs(-5)10000000 loops, best of 3: 194 ns per loop

So abs() is faster than math.fabs().

 
3  
You're not comparing apples to apples there. Use from math import fabs for sure, and try -5.0 for both. –   
May 27 '12 at 7:25 
 
@agf Thanks for pointing it out! That was dumb of me. –   
May 27 '12 at 7:31 
 
Also unreliable timing results? You first had abs(-5) at 102ns, then later show it as 88.3ns. Never draw conclusions from a single run of any benchmark, even if it internally tries to avoid the issues as timeit does. –   
Jan 3 '13 at 2:11
 
Two more points: this still compares apples and oranges, since abs is looked up in the builtins, while fabs is at module namespace. Do "xabs = abs" then xabs(num) to remove that effect. Also, with Python 3.2 things change quite a bit and apparently abs() is quite a bit faster (>2x), at least on floats. –   
Jan 3 '13 at 2:14
 
@PeterHansen You are right, those are from two runs under different system loads. Though when compared within the same set of tests, the relative difference is still valid. Also, thanks for pointing out the namespace difference -- I didn't consider that. I haven't tried it on 3.2, but that's good to know! I will update my answer with your suggestions a bit later :) Thanks again! –   
Jan 3 '13 at 2:20

转载地址:http://xihbi.baihongyu.com/

你可能感兴趣的文章
Xpath使用方法
查看>>
移动端自动化测试-Mac-IOS-Appium环境搭建
查看>>
Selenium之前世今生
查看>>
Selenium-WebDriverApi接口详解
查看>>
Selenium-ActionChains Api接口详解
查看>>
Selenium-Switch与SelectApi接口详解
查看>>
Selenium-Css Selector使用方法
查看>>
Linux常用统计命令之wc
查看>>
测试必会之 Linux 三剑客之 sed
查看>>
Socket请求XML客户端程序
查看>>
Java中数字转大写货币(支持到千亿)
查看>>
Java.nio
查看>>
函数模版类模版和偏特化泛化的总结
查看>>
VMware Workstation Pro虚拟机不可用解决方法
查看>>
最简单的使用redis自带程序实现c程序远程访问redis服务
查看>>
redis学习总结-- 内部数据 字符串 链表 字典 跳跃表
查看>>
iOS 对象序列化与反序列化
查看>>
iOS 序列化与反序列化(runtime) 01
查看>>
iOS AFN 3.0版本前后区别 01
查看>>
iOS ASI和AFN有什么区别
查看>>