java 初始化填充数组_NumPy数组初始化(填充相同的值)

我有

numpy.array(n * [value])

记住,但显然这比所有其他建议要慢得多 n .

这里与perfplot(我的宠物项目)进行了全面比较 .

b670bdbc-02c0-410e-8ddb-6338ef8b40ff.png

两个 empty 替代品仍然是最快的(使用NumPy 1.12.1) . full 赶上大型数组 .

生成图的代码:

import numpy as np

import perfplot

def empty_fill(n):

a = np.empty(n)

a.fill(3.14)

return a

def empty_colon(n):

a = np.empty(n)

a[:] = 3.14

return a

def ones_times(n):

return 3.14 * np.ones(n)

def repeat(n):

return np.repeat(3.14, (n))

def tile(n):

return np.repeat(3.14, [n])

def full(n):

return np.full((n), 3.14)

def list_to_array(n):

return np.array(n * [3.14])

perfplot.show(

setup=lambda n: n,

kernels=[

empty_fill, empty_colon, ones_times, repeat, tile, full, list_to_array

],

n_range=[2**k for k in range(27)],

xlabel='len(a)',

logx=True,

logy=True,

)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值