小编Sti*_*ggo的帖子

在Python中创建随机的整数列表

我想为测试目的创建一个随机的整数列表.数字的分布并不重要.唯一重要的是时间.我知道生成随机数是一项耗时的任务,但必须有更好的方法.

这是我目前的解决方案:

import random
import timeit

# Random lists from [0-999] interval
print [random.randint(0, 1000) for r in xrange(10)] # v1
print [random.choice([i for i in xrange(1000)]) for r in xrange(10)] # v2

# Measurement:
t1 = timeit.Timer('[random.randint(0, 1000) for r in xrange(10000)]', 'import random') # v1
t2 = timeit.Timer('random.sample(range(1000), 10000)', 'import random') # v2

print t1.timeit(1000)/1000
print t2.timeit(1000)/1000
Run Code Online (Sandbox Code Playgroud)

v2比v1快,但它的工作规模不大.它给出以下错误:

ValueError:大于人口的样本

是否有一个快速,有效的解决方案,以这种规模工作?

一些结果来自答案

安德鲁:0.000290962934494

gnibbler's:0.0058455221653

KennyTM's:0.00219276118279

NumPy来了,看到并征服了.

python random performance list

68
推荐指数
3
解决办法
16万
查看次数

允许不使用str.split()的字符串的反向字顺序

这样做的pythonic方法是什么?

从这个:'这是一个尝试'的字符串''尝试字符串a是这个'

我的第一个猜测是:

for w in 'This is a string to try'.split(' ')[::-1]:
    print w,
Run Code Online (Sandbox Code Playgroud)

但是str.split()是不允许的.然后我想出了这个:

def reverse_w(txt):
    tmp = []
    while (txt.find(' ') >= 0):
        tmp.append(txt[:txt.find(' ')])
        txt = txt[txt.find(' ')+1:]
    if (txt.find(' ') == -1):
        tmp.append(txt)
   return tmp[::-1]
Run Code Online (Sandbox Code Playgroud)

python string

5
推荐指数
1
解决办法
5695
查看次数

硬件启发循环.废话?

前几天我在Verilog学到了一个很酷的技巧.当你需要反复做某事时.您可以使用移位寄存器来计算增量数.只需将1从LSB移到MSB,当它到达MSB时就完成了.

在C中它将是这样的:

for(j=0b1; !(j & (1<<16)); j=j<<1)
{
/*do a thing 16 times*/
}
Run Code Online (Sandbox Code Playgroud)

我知道它由于位宽而限制使用,但它不涉及任何添加因此它很快.所以我的问题:这有什么用吗?在C语言或任何其他高级语言中使用是否值得?

也许在资源有限的嵌入式系统中.

谢谢

c embedded performance loops verilog

5
推荐指数
3
解决办法
335
查看次数

Perl DBI连接并执行超时

在工作中我们有一位DBA,他说他的RAC工作得很好,但事实是事实并非如此.像Toad或SQL Developer这样的SQL IDE会随机丢弃它们的连接(我怀疑是因为RAC的网络设置不正确).我想通过测试来证明我的理论.我想perl脚本可以解决问题:

步骤1. ping数据库的IP

步骤2.如果IP正在尝试连接到数据库

步骤3.如果连接,则从双连接和关闭连接中选择sysdate

第4步.等一段时间再重新开始

我已经设法使用DBI在Perl中编写了这个,但我不知道如何超时连接和查询执行.是否有一些解决方案来超时这些东西?

perl dbi oracle11g

4
推荐指数
2
解决办法
6547
查看次数

Python计时 - 必须有更好的方法!

我希望有人可以帮助我解决这个问题.我想测量排序算法.这是我目前的做法:

M = 1000 # number of executions
N = [1000, 2000, 4000, 16000] # size of the list
L = [100, 1000, 2000,16000] # max element of the list

# timing:
print 'Number of executions: %i' % (M)
print '-'*80
print '\tL\N\t|\t%i\t|\t%i\t|\t%i\t|\t%i' % (N[0], N[1], N[2], N[3])
print '-'*80
for l in L:
    print '\t%i\t' % l,
    for n in N: 
        t = 0
        for m in xrange(M):
            A = [random.randint(0,l-1) for r in xrange(n)] # generates an n …
Run Code Online (Sandbox Code Playgroud)

python testing performance timing

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

标签 统计

performance ×3

python ×3

c ×1

dbi ×1

embedded ×1

list ×1

loops ×1

oracle11g ×1

perl ×1

random ×1

string ×1

testing ×1

timing ×1

verilog ×1