【python效率优化】使用map优化for循环

2023-05-16

python提供的高级函数map将一个函数作用于可迭代对象的每一个元素,底层自动实现并行,运行速度比for循环要快,对于无前后联系的for循环,可以使用map进行优化,以下例子对比了两者的运行速度,map的速度优于for循环

import numpy as np
import time

arr = np.arange(0, 10000000)

t1 = time.time()
res1 = []
for i in range(len(arr)):
    res1.append(arr[i]+4)
print("for loop times", time.time()-t1)

t2 = time.time()
res2 = list(map(lambda x: x+4, arr))
print("map times", time.time()-t2)

print("res1:", res1[:6])
print("res2:", res2[:6])

for loop times 5.5192711353302
map times 4.321439027786255
res1: [4, 5, 6, 7, 8, 9]
res2: [4, 5, 6, 7, 8, 9]

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【python效率优化】使用map优化for循环 的相关文章

随机推荐

  • web控件开发

    https www bbsmax com A gGdX6v11J4 https open chrome 360 cn extension dev overview html 扩展示例 Mozilla MDN https edge micro
  • 【string】获取字符串长度strlen

    span class token keyword char span str span class token punctuation span span class token punctuation span span class to
  • 【string】int转string to_string

    使用to string函数将整型变量转为字符串 span class token keyword int span n span class token operator 61 span span class token number 10
  • 【C++】string、char*互相转换

    string 2 char c str 方法返回一个const char 类型的指针变量 xff0c 使用strcpy函数copy string str span class token operator 61 span span clas
  • 【string】字符串分割strock

    使用strock将字符串按特定分隔符分割 span class token macro property span class token directive keyword include span span class token st
  • 【string】字符串转int stoi

    使用stoi函数将字符串转为int型 xff0c 需要 include lt string gt span class token keyword char span chs span class token punctuation spa
  • Visual Studio解决const char *与LPCWSTR 不兼容

    项目 gt 属性 gt 配置属性 gt 高级 xff0c 将字符集改为未设置
  • 【C++】文件读写指针定位

    主要函数 xff1a 指针定位函数SetFilePointer xff0c 读文件ReadFile xff0c 写文件WriteFile 首先使用CreateFile创建文件 xff0c SetFilePointer函数将指针定位到文件指定
  • 【C++】程序计时

    span class token macro property span class token directive keyword include span span class token string lt iostream gt s
  • 【双指针】15.三数之和

    题目 给你一个包含 n 个整数的数组 nums xff0c 判断 nums 中是否存在三个元素 a xff0c b xff0c c xff0c 使得 a 43 b 43 c 61 0 xff1f 请你找出所有满足条件且不重复的三元组 注意
  • 函数定义中的冒号和箭头

    函数中变量后面加冒号和类型表示该参数的建议类型 xff0c 如下参数A的建议类型是int xff0c 参数B的建议类型是str xff0c gt 表示该函数的返回值类型 xff0c 例如fun函数的返回值类型是str span class
  • chrom控件命令行加载

    34 C Users lt name gt AppData Local Google Chrome Application chrome exe 34 load extension 61 34 lt path to unpacked ext
  • 【二叉树】创建、先序遍历、中序遍历、后序遍历、层序遍历 Java实现

    二叉树的创建 二叉树的创建可以采用递归方式 xff0c 传入一个数组 xff0c 例如数组 3 9 20 null null 15 7 表示的二叉为 span class token number 3 span span class tok
  • 【二叉树】平衡二叉树

    平衡二叉树是一种二叉查找树又称为AVL树 Adelsen Velskii and Landis xff0c 特点为每个节点的左 右子树深度之差的绝对值不大于1 xff0c 左子树的值小于右子树 xff0c 重要操作为插入和删除 插入 插入新
  • win10搭建hadoop2.7.7

    配置java环境 配置java环境 xff0c 官网下载jdk较慢 xff0c 百度网盘 xff1a 链接 xff1a https pan baidu com s 1wX7LxPMjcS9QGc4c4cPJgw 提取码 xff1a 9e38
  • win10配置spark

    下载spark压缩包 xff0c 链接 xff1a https pan baidu com s 1y5JlMdtkrZFyTJWKtuuZ Q 提取码 xff1a z64y 解压tar gz文件 配置环境变量 xff0c 系统变量Path中
  • 【HDFS】上传、查看、下载、删除文件命令

    上传 首先启动HDFS xff0c 任意目录下输入命令start dfs xff08 若没有配置sbin的环境变量则需要在sbin目录下打开cmd输入该命令 xff09 xff0c 出现以下两个框框 在需要上传文件的文件路径下打开cmd命令
  • IntelliJ IDEA开发spark应用(scala)

    配置spark环境 xff0c 可参考官网下载 IntelliJ IDEA xff0c 然后安装 xff0c 一直next即可 安装Scala插件 创建一个新工程 Ctrl 43 Shift 43 Alt 43 s xff0c 导入spar
  • 【STL】vector简单使用

    参考 需要头文件 span class token macro property span class token directive keyword include span span class token string lt iost
  • 【python效率优化】使用map优化for循环

    python提供的高级函数map将一个函数作用于可迭代对象的每一个元素 xff0c 底层自动实现并行 xff0c 运行速度比for循环要快 xff0c 对于无前后联系的for循环 xff0c 可以使用map进行优化 xff0c 以下例子对比