当前位置:网站首页>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 .
边栏推荐
- JSON parsing, esp32 easy access to time, temperature and weather
- NLP: brief introduction of transformer in NLP natural language field (pre training technology), NLP model development (elmo/gpt/bert/mt-dnn/xlnet/roberta/albert), detailed introduction to classic case
- 乔治·华盛顿大学 : Hanhan Zhou | PAC:多智能体强化学习中具有反事实预测的辅助价值因子分解
- Object access mechanism and others
- Operating instructions and Q & A of cec-i China learning machine
- 简单学习GoogleColab的入门级概念
- Statistical Hypothesis Testing
- Did your case really pass?
- SystemVerilog simulation speed increase
- cookie,sessionstorage,localstorage区别
猜你喜欢

博日科技招股书失效,中金公司已停止对其辅导,放弃港交所上市?

Daily question brushing record (V)

How to convert an old keyboard into a USB keyboard and program it yourself?

Meituan: data management and pit avoidance strategy summarized after stepping on Thunder for several years

The world is very big. Some people tattoo QR codes on their necks

Custom MVC (imported into jar package) + difference from three-tier architecture + reflection + interview questions

每日刷题记录 (五)

One click acceleration of Sony camera SD card file copy operation, file operation batch processing tutorial

uvm中的config机制方法总结(二)

疫情期间居家办公的总结体会 |社区征文
随机推荐
UVM in UVM_ config_ Use of DB in sequence
Database interview questions +sql statement analysis
Topolvm: kubernetes local persistence scheme based on LVM, capacity aware, dynamically create PV, and easily use local disk
Keepalived 实现 Redis AutoFailover (RedisHA)12
Online text digit recognition list summation tool
leetcode 1143. Longest Commom Subsequence 最长公共子序列(中等)
I encountered some problems when connecting to the database. How can I solve them?
Memcached foundation 6
Custom jsp[if, foreach, data, select] tag
SystemVerilog仿真速率提升
Two days of beautiful butterfly animation
cookie,sessionstorage,localstorage区别
XSS attack notes (Part 1)
Interface test framework practice (I) | requests and interface request construction
Keepalived 实现 Redis AutoFailover (RedisHA)17
UVM中config_db机制的使用方法
Esp32-solo development tutorial to solve config_ FREERTOS_ UNICORE problem
UVM in UVM_ report_ Enabled usage
Beyond lithium battery -- the concept of battery in the future
uvm中的config机制方法总结(二)