Project1
标题:
脚本问题
[打印本页]
作者:
ae3175001
时间:
2013-10-2 23:07
标题:
脚本问题
0.1 <= a <= 0.5
如何用脚本表达a在0.1到0.5之间,最小时为0.1最大时为0.5 ,求简洁的脚本代码
作者:
喵呜喵5
时间:
2013-10-2 23:27
a.between?(0.1,0.5)
(0.1..0.5)===a
(0.1..0.5).member?a
(0.1..0.5).include?a
作者:
xTsukihime
时间:
2013-10-3 01:35
本帖最后由 xTsukihime 于 2013-10-3 01:40 编辑
我建議
0.1 <= a && a <= 0.5
复制代码
意思是 ”0.1<= a和 a <= 0.5“
而且比別的快2-4倍
require 'Benchmark'
a = 0.2
n = 100000
Benchmark.bm {|x|
x.report { n.times{a.between?(0.1,0.5)}}
x.report { n.times{(0.1..0.5)===a}}
x.report { n.times{(0.1..0.5).member?a}}
x.report { n.times{(0.1..0.5).include?a}}
x.report { n.times{ 0.1 <= a && a <= 0.5}}
}
复制代码
user system total real
0.047000 0.000000 0.047000 ( 0.034002)
0.078000 0.000000 0.078000 ( 0.088005)
0.078000 0.000000 0.078000 ( 0.081004)
0.093000 0.000000 0.093000 ( 0.081005)
0.016000 0.000000 0.016000 ( 0.025001)
复制代码
作者:
tan12345
时间:
2013-10-3 10:14
如果是取值范围,a=(rand(5)+1)/10
如果是判断,if a>=0.1 and a<=0.5
欢迎光临 Project1 (https://rpg.blue/)
Powered by Discuz! X3.1