通过shell脚本计算圆周率,可以简单的测试CPU。
jerome@ubuntu:~$ time echo "scale=100; a(1)*4" | bc -l 3.141592653589793238462643383279502884197169399375105820974944592307\ 8164062862089986280348253421170676 real 0m0.052s user 0m0.011s sys 0m0.027s jerome@ubuntu:~$
通过 man bc 可以查看 -l 参数的意思是:
-l, --mathlib Define the standard math library.
而函数 a(x) 则是:
a (x) The arctangent of x, arctangent returns radians.
所以,这里是利用了 bc 命令中的 反正切函数(arctan),因为在反三角函数中:
tan(π/4) = 1 ==> π = 4 * arctan(1)
而 scale 显然是小数点后的位数(即小数精度)。
scale ( expression ) The value of the scale function is the number of digits after the decimal point in the expression.
一条评论
謝謝分享!