当前位置:网站首页>On the operation mechanism of numpy array
On the operation mechanism of numpy array
2022-06-27 01:40:00 【Colonization armor Kemp】
List of articles
Preface
numpy The array can be directly operated with a scalar to realize the operation by elements , Very concise , But if this scalar is also a function , How to write more efficiently ? Such as normalization , Array / Array and , It's direct /np.sum( Array ) Or do you get the array first and then do the operation ?
experiment
The way a Namely /np.sum( Array ), The way b Is to get the array first and then operate , Because the array size is limited , Here, several iterations have been carried out to widen the gap :
import numpy as np
import time
def timer(start, name):
duration = time.time() - start
time_list = [0, 0, 0]
time_list[0] = duration // 3600
time_list[1] = (duration % 3600) // 60
time_list[2] = round(duration % 60, 2)
print(name + ' when :' + str(time_list[0]) + ' when ' + str(time_list[1]) + ' branch ' + str(time_list[2]) + ' second ')
def a(nums):
nums = nums / np.sum(nums)
def b(nums):
sum_ = np.sum(nums)
nums = nums / sum_
x = np.random.randn(500000000)
iter = 100
s = time.time()
for _ in range(iter):
a(x)
timer(s, 'a')
s = time.time()
for _ in range(iter):
b(x)
timer(s, 'b')
Output :
a when :0.0 when 3.0 branch 34.98 second
b when :0.0 when 3.0 branch 11.88 second
It can be seen that it is faster to sum first and then calculate .
Because I didn't find it numpy Implementation mechanism , So observe through experiments , If you have any friends who understand the mechanism, please don't hesitate to comment .
边栏推荐
- The world is very big. Some people tattoo QR codes on their necks
- Great vernacular with high concurrency (I)
- 疫情期间居家办公的总结体会 |社区征文
- 清华&智源 | CogView2:更快更好的文本图像生成模型
- uvm中的config机制方法总结(一)
- Kept to implement redis autofailover (redisha) 11
- The most difficult 618 in history, TCL won the first place in both jd.com and tmall.com shares in the TV industry
- 【系统分析师之路】第六章 复盘需求工程(案例论文)
- Processing of slice loss in ArcGIS mosaic dataset
- XSS notes (Part 2)
猜你喜欢

Bs-gx-016 implementation of textbook management system based on SSM

30《MySQL 教程》MySQL 存储引擎概述

Structure the fifth operation of the actual camp module

史上最难618,TCL夺得电视行业京东和天猫份额双第一

世界很大,有人把二维码纹在脖子上

Topolvm: kubernetes local persistence scheme based on LVM, capacity aware, dynamically create PV, and easily use local disk

浏览器缓存

建模规范:环境设置

通过Rust语言计算加速技术突破图片识别性能瓶颈

Summary of working at home during the epidemic | community essay solicitation
随机推荐
清华&智源 | CogView2:更快更好的文本图像生成模型
cookie,sessionstorage,localstorage区别
JVM 的指针压缩
1.44寸TFT-LCD显示屏取模教程
UVM中uvm_config_db非直线的设置与获取
Summary of config mechanism and methods in UVM (2)
markdown表格(合并)
memcached基础
Cookie, sessionstorage, localstorage differences
buuctf-pwn write-ups (6)
Browser cache
Custom MVC (imported into jar package) + difference from three-tier architecture + reflection + interview questions
leetcode 1143. Longest Commom Subsequence 最长公共子序列(中等)
美团:踩雷好几年,才总结出的数据治理避坑攻略
博日科技招股书失效,中金公司已停止对其辅导,放弃港交所上市?
SystemVerilog simulation speed increase
Custom class loader encrypts and decrypts classes
Two days of beautiful butterfly animation
CLIP:从自然语言监督中学习可迁移的视觉模型
SystemVerilog仿真速率提升