- 主题:如何优雅的生成类似[1,-3,5,-7……]这样的序列
见51楼
【 在 KDr2 的大作中提到: 】
: Python 版呢?
:
--
FROM 60.1.11.*
图片。。。
【 在 sosei (fss.sosei) 的大作中提到: 】
: 见51楼
--
FROM 123.139.18.*
呃呃呃,遇上打不开图的错误啦
破水木
from itertools import islice, chain, count
from math import ceil
a = 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
%timeit list(islice(chain.from_iterable(zip(count(a, 4), count(-(a+2), -4))), ceil(1000000 / 2)))
0.184s
【 在 KDr2 的大作中提到: 】
: 图片。。。
:
:
--
FROM 60.1.11.*
同一台机器上,
Python:
In [7]: a,b = 99999999999999999999999999999999999999999999999999999999999999
9999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999
...: 9999999999999999999999999999999999999999999999999999,999999
In [8]: from itertools import islice, chain, count
In [9]: %timeit list(islice(chain.from_iterable(zip(count(a, 4), count(-(a+2
), -4))), b//2))
82.8 ms ± 1.89 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
Julia:
julia> function f(b,l)
a = Vector{BigInt}(undef, Int(ceil((l+1)/2)))
p = 1
@inbounds for i = b:4:b+l
a[p] = i
p += 2
end
p = 2
@inbounds for i = -(b+2):-4:-(b+l)
a[p] = i
p += 2
end
a
end
f (generic function with 1 method)
julia> a,b = 999999999999999999999999999999999999999999999999999999999999999
9999999999999999999999999999999999999999999999999999999999999999999999999999
99999999999999999999999999999999999999999999999999999999999999999999999,9999
99;
julia> f(2,2);
julia> BenchmarkTools.@btime f(a, b);
50.668 ms (1500039 allocations: 64.85 MiB)
julia>
-----------------------
换了跟 %timeit 一样多次取样的 @btime 做 benchmark。
Julia 的 BigInt 用的是 GMP, vector里做不到内存连续,优势不像 primitive int
那么大。
【 在 sosei (fss.sosei) 的大作中提到: 】
: 呃呃呃,遇上打不开图的错误啦
: 破水木
: from itertools import islice, chain, count
: ...................
--
FROM 123.139.18.*
这个赞
【 在 superisaac (宅男总动员) 的大作中提到: 】
: def gen(x):
: a, b, c = 5, -3, 1
: for _ in range(x):
: ...................
--
FROM 115.57.128.*
嗯嗯学到两个宏
在我这酷睿2 win7上跑,时间没啥变化。可能老型号CPU优化不行
【 在 KDr2 的大作中提到: 】
: 同一台机器上,
: Python:
: In [7]: a,b = 99999999999999999999999999999999999999999999999999999999999999
: ...................
--
FROM 60.1.11.*