为了回答你的问题,我玩了一些变体,并对它们进行了分析。

结论:将数据从一个numpy数组复制到另一个使用内置的numpy函数numpy.array(src)或numpy.copyto(dst, src) 。

(但是如果dst的内存已经被分配了,总是select后面的内存来重用内存。

分析设置

import timeit import numpy as np import pandas as pd from IPython.display import display def profile_this(methods, setup='', niter=10**4, p_globals=None, **kwargs): if p_globals is not None: print('globals: {0}, tested {1:.0e} times'.format(p_globals, niter)) timings = np.array([timeit.timeit(method, setup=setup, number=niter, globals=p_globals, **kwargs) for method in methods]) ranking = np.argsort(timings) timings = np.array(timings)[ranking] methods = np.array(methods)[ranking] speedups = np.max(timings) / timings pd.set_option('html', False) data = {'time (s)': timings, 'speedup': ['{0:0.2f}x'.format(s) if 1 != s else '' for s in speedups], 'methods': methods} data_frame = pd.DataFrame(data, columns=['time (s)', 'speedup', 'methods']) display(data_frame) print()

分析代码

setup = '''import numpy as np; x = np.random.random(n)''' methods = ( '''y = np.zeros(n, dtype=x.dtype); y[:] = x''', '''y = np.zeros_like(x); y[:] = x''', '''y = np.empty(n, dtype=x.dtype); y[:] = x''', '''y = np.empty_like(x); y[:] = x''', '''y = np.copy(x)''', '''y = x.astype(x.dtype)''', '''y = 1*x''', '''y = np.empty_like(x); np.copyto(y, x)''', '''y = np.empty_like(x); np.copyto(y, x, casting='no')''', '''y = np.empty(n)\nfor i in range(x.size):\n\ty[i] = x[i]''' ) for n, it in ((2, 6), (3, 6), (3.8, 6), (4, 6), (5, 5), (6, 4.5)): profile_this(methods[:-1:] if n > 2 else methods, setup, int(10**it), {'n': int(10**n)})

Windows 7在Intel i7 CPU,CPython v3.5.0,numpy v1.10.1上的结果。

globals: {'n': 100}, tested 1e+06 times time (s) speedup methods 0 0.386908 33.76xy = np.array(x) 1 0.496475 26.31xy = x.astype(x.dtype) 2 0.567027 23.03xy = np.empty_like(x); np.copyto(y, x) 3 0.666129 19.61xy = np.empty_like(x); y[:] = x 4 0.967086 13.51xy = 1*x 5 1.067240 12.24xy = np.empty_like(x); np.copyto(y, x, casting=... 6 1.235198 10.57xy = np.copy(x) 7 1.624535 8.04xy = np.zeros(n, dtype=x.dtype); y[:] = x 8 1.626120 8.03xy = np.empty(n, dtype=x.dtype); y[:] = x 9 3.569372 3.66xy = np.zeros_like(x); y[:] = x 10 13.061154 y = np.empty(n)\nfor i in range(x.size):\n\ty[... globals: {'n': 1000}, tested 1e+06 times time (s) speedup methods 0 0.666237 6.10xy = x.astype(x.dtype) 1 0.740594 5.49xy = np.empty_like(x); np.copyto(y, x) 2 0.755246 5.39xy = np.array(x) 3 1.043631 3.90xy = np.empty_like(x); y[:] = x 4 1.398793 2.91xy = 1*x 5 1.434299 2.84xy = np.empty_like(x); np.copyto(y, x, casting=... 6 1.544769 2.63xy = np.copy(x) 7 1.873119 2.17xy = np.empty(n, dtype=x.dtype); y[:] = x 8 2.355593 1.73xy = np.zeros(n, dtype=x.dtype); y[:] = x 9 4.067133 y = np.zeros_like(x); y[:] = x globals: {'n': 6309}, tested 1e+06 times time (s) speedup methods 0 2.338428 3.05xy = np.array(x) 1 2.466636 2.89xy = x.astype(x.dtype) 2 2.561535 2.78xy = np.empty_like(x); np.copyto(y, x) 3 2.603601 2.74xy = np.empty_like(x); y[:] = x 4 3.005610 2.37xy = np.empty_like(x); np.copyto(y, x, casting=... 5 3.215863 2.22xy = np.copy(x) 6 3.249763 2.19xy = 1*x 7 3.661599 1.95xy = np.empty(n, dtype=x.dtype); y[:] = x 8 6.344077 1.12xy = np.zeros(n, dtype=x.dtype); y[:] = x 9 7.133050 y = np.zeros_like(x); y[:] = x globals: {'n': 10000}, tested 1e+06 times time (s) speedup methods 0 3.421806 2.82xy = np.array(x) 1 3.569501 2.71xy = x.astype(x.dtype) 2 3.618747 2.67xy = np.empty_like(x); np.copyto(y, x) 3 3.708604 2.61xy = np.empty_like(x); y[:] = x 4 4.150505 2.33xy = np.empty_like(x); np.copyto(y, x, casting=... 5 4.402126 2.19xy = np.copy(x) 6 4.917966 1.96xy = np.empty(n, dtype=x.dtype); y[:] = x 7 4.941269 1.96xy = 1*x 8 8.925884 1.08xy = np.zeros(n, dtype=x.dtype); y[:] = x 9 9.661437 y = np.zeros_like(x); y[:] = x globals: {'n': 100000}, tested 1e+05 times time (s) speedup methods 0 3.858588 2.63xy = x.astype(x.dtype) 1 3.873989 2.62xy = np.array(x) 2 3.896584 2.60xy = np.empty_like(x); np.copyto(y, x) 3 3.919729 2.58xy = np.empty_like(x); np.copyto(y, x, casting=... 4 3.948563 2.57xy = np.empty_like(x); y[:] = x 5 4.000521 2.53xy = np.copy(x) 6 4.087255 2.48xy = np.empty(n, dtype=x.dtype); y[:] = x 7 4.803606 2.11xy = 1*x 8 6.723291 1.51xy = np.zeros_like(x); y[:] = x 9 10.131983 y = np.zeros(n, dtype=x.dtype); y[:] = x globals: {'n': 1000000}, tested 3e+04 times time (s) speedup methods 0 85.625484 1.24xy = np.empty_like(x); y[:] = x 1 85.693316 1.24xy = np.empty_like(x); np.copyto(y, x) 2 85.790064 1.24xy = np.empty_like(x); np.copyto(y, x, casting=... 3 86.342230 1.23xy = np.empty(n, dtype=x.dtype); y[:] = x 4 86.954862 1.22xy = np.zeros(n, dtype=x.dtype); y[:] = x 5 89.503368 1.18xy = np.array(x) 6 91.986177 1.15xy = 1*x 7 95.216021 1.11xy = np.copy(x) 8 100.524358 1.05xy = x.astype(x.dtype) 9 106.045746 y = np.zeros_like(x); y[:] = x

python 数组拷贝,如何将数据从一个numpy数组复制到另一个数组相关推荐

  1. C语言试题四十六之将m行n列的二维数组中的字符数据,按列的顺序依次放到一个字符串中。

    1. 题目 请编写一个函数function,它的功能是:将m行n列的二维数组中的字符数据,按列的顺序依次放到一个字符串中. 2 .温馨提示 C语言试题汇总里可用于计算机二级C语言笔试.机试.研究生复试 ...

  2. u盘到计算机复制不了,为什么电脑程序从一个u盘复制到另一个u盘后就不能用

    为什么电脑程序从一个u盘复制到另一个u盘后就不能用以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 为什么电脑程序从一个u ...

  3. Word控件Spire.Doc 【段落处理】教程(五):如在 C#、VB.NET 中将 Word 段落从一个文档复制到另一个文档

    复制 Word 段落时,用户可以选择仅复制文本或复制段落的所有元素,例如格式.图像.超链接等.本指南中的解决方案介绍了如何在 C# 中将 Word 段落(文本和格式)从一个文档复制到另一个文档和VB. ...

  4. c#中将一个实体类复制到另一个实体类

    根据网上现有的做了一些更改,参考源: https://blog.csdn.net/qq719365064/article/details/52925456 /// <summary>   ...

  5. oracle复制表到mysql_oracle – 如何将数据从一个数据库/表复制到另一个数据库/表...

    在典型的Oracle环境中,您已经设置了TNS名称.这是一个服务,用于查找给定SID或服务名称的Oracle实例的连接参数.在最简单的形式中,TNS名称是由环境变量TNS_ADMIN(指向文件所在目录 ...

  6. linux 由一个文件夹复制到另外一个文件夹

    cp -Rf /home/user1/* /root/temp/ 将 /home/user1目录下的所有东西拷到/root/temp/下而不拷贝user1目录本身. 即格式为:cp -Rf 原路径/ ...

  7. ubuntu 将某个目录下的文件复制到_命令行 将多个特定文件从一个文件夹复制到另一个文件夹...

    只需从命令行一次复制多个文件 有几种方法可以实现这个,我见过的最容易的是cp /home/usr/dir/{file1,file2,file3,file4} /home/usr/destination ...

  8. Excel一个工作表复制到另一个工作簿开头的位置

    如下图有A和B2个工作簿,现在要分享的是,把A工作簿中的例子工作表,复制到B工作簿中的开头位置,该如何操作呢 (方方格子插件) 1.先看动图演示吧 2.我们在A表中选择方方格子按钮 3.选择工作表下拉 ...

  9. 二维数组 赋值_3.9数组(数组基本使用、数组的循环、数组拷贝、数组排序、多维数组)...

    3.9数组 3.9.1数组基本使用 数组,英文叫Array,是一种数据结构,是用来存放同一数据类型数值的集合.例如存放30个int型数值.存放100个double型数值等等. 我们知道使用一个变量,需 ...

最新文章

  1. 第二十五讲 用线性代数解微分方程组
  2. 10 个让你相见恨晚的 Python 骚操作
  3. IE下列表框不能给option绑定click事件的解决办法
  4. JAVA TCP/IP网络通讯编程(二)
  5. getchar()和EOF总结
  6. .NET Core开发日志——OData
  7. NOIP2018 游记
  8. java.lang.NumberFormatException: Infinite or NaN原因之浮点类型除数为0
  9. Windows2000系统下Apache2和PHP4安装终级宝典
  10. 零基础学sql要多久_零基础如何学习游戏3D建模,要学些什么内容?学多久?
  11. python项目方案书模板格式_项目计划书格式范本word文档
  12. 简述计算机硬件,简述计算机硬件的组成部分
  13. 使用python embedded distribution
  14. 1000句英语经典口语(3)
  15. MEM/MBA 写作-论说文(01)概述
  16. 磁盘管理之 raid 文件系统 分区
  17. 手淘双十一性能优化项目揭秘
  18. eclipse导入已存在工程报 Faceted Project Problem 错误
  19. 什么是 Kubernetes HPA
  20. my-view-isnt-reflecting-changes-ive-made-to-the-underlying-tables

热门文章

  1. Go语言拼接URL路径的三种方法
  2. Python练习题及答案
  3. 17.6 unique_lock详解
  4. 测试代码(Python)
  5. CPU系统级验证——概览索引
  6. 刘强东“退位”,东哥的兄弟们怎么办?| 文末送书
  7. Node.js实现登录注册
  8. 【BZOJ3527】【FFT】力
  9. 坚持这九件事,你会越来越顺!
  10. 补肾的食物有哪些,吃什么补肾,专家建议初冬宜补肾