What is fastest way to compute average of python array different elements with condition?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


What is fastest way to compute average of python array different elements with condition?



I have phyton code below which executed about 2s.
Equivalent code in C language executed in 31ms.


#!/usr/bin/env python

import time
import numpy as np


dd = np.random.randint(0, 20, size=(2*1000*1000))

t0= time.clock()

avg_sum1=0.0
BlockOffset = 0
while BlockOffset < len(dd):
if dd[BlockOffset + 1] <= 10:
avg_sum1 = dd[BlockOffset + 1] * 0.1
else:
avg_sum1 = dd[BlockOffset + 0] * 0.01
BlockOffset+=2

print('Avg: ' + str( avg_sum1/len(dd)/2 ) )
print('Exe time: '+ str(time.clock() - t0) )



What is fastest way to to do this with built in functions or numpy?





Although the question may be on-topic here, Code Review might be more suitable for your demonstrated needs.
– Jerrybibo
58 secs ago











By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived