小编Ron*_*ohn的帖子

在具有240个或更多元素的数组上循环时,为什么会对性能产生较大影响?

当在Rust中的一个数组上运行求和循环时,我发现CAPACITY> = 240 时性能会大幅下降。CAPACITY= 239的速度大约是80倍。

Rust对“短”数组进行了特殊的编译优化吗?

与编译rustc -C opt-level=3

use std::time::Instant;

const CAPACITY: usize = 240;
const IN_LOOPS: usize = 500000;

fn main() {
    let mut arr = [0; CAPACITY];
    for i in 0..CAPACITY {
        arr[i] = i;
    }
    let mut sum = 0;
    let now = Instant::now();
    for _ in 0..IN_LOOPS {
        let mut s = 0;
        for i in 0..arr.len() {
            s += arr[i];
        }
        sum += s;
    }
    println!("sum:{} time:{:?}", sum, …
Run Code Online (Sandbox Code Playgroud)

arrays performance rust llvm-codegen

214
推荐指数
2
解决办法
2万
查看次数

python 3 替代 dircache?

在我重新发明轮子之前,谁能告诉我是否有单行语句的直接(或半直接)替代:

allfiles = dircache.listdir('.')
Run Code Online (Sandbox Code Playgroud)

python python-2.7 python-3.x

6
推荐指数
1
解决办法
4037
查看次数

新的Python打印格式语句返回不同的结果.为什么?

Python 2.7.5+

新样式"{X.Yf}".format(num)似乎不像旧样式'%X.Yf'%(num).谁能解释一下?

>>> '%8.3f' % (0.98567)
'   0.986'
>>> '%8.3f' % (1.98567)
'   1.986'

>>> '{num:8.3}'.format(num=0.98567)
'   0.986'
>>> '{num:8.3}'.format(num=1.98567)
'    1.99'
Run Code Online (Sandbox Code Playgroud)

请注意旧样式如何在小数点后显示3位数,但新样式有时会打印2,有时为3.我是否犯了一些愚蠢的错误?

python string string-formatting python-2.7

3
推荐指数
1
解决办法
327
查看次数