Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Best way to do

23 views
Skip to first unread message

pauld11718

unread,
Apr 25, 2015, 6:37:40 AM4/25/15
to
import CoolProp.CoolProp as CPpy
import numpy as np
import time

len = 1000
rho = np.zeros(len)
t = np.zeros(len)
eta = np.zeros(len)
x = np.zeros(len)

def PyDir(p,h,hndl):
for i in range(1, len):
rho[i] = hndl.PropsSI("D","P",p[i],"H",h[i],"Water")
t[i] = hndl.PropsSI('T','P',p[i],'H',h[i],'Water')
x[i] = hndl.PropsSI('Q','P',p[i],'H',h[i],'Water')
eta[i] = hndl.PropsSI('V','P',p[i],'H',h[i],'Water')
return rho,t,eta,x

def PyDirn(p,h,hndl):
rho = hndl.PropsSI("D","P",p,"H",h,"Water")
t = hndl.PropsSI('T','P',p,'H',h,'Water')
x = hndl.PropsSI('Q','P',p,'H',h,'Water')
eta = hndl.PropsSI('V','P',p,'H',h,'Water')
return rho,t,eta,x

def PyDirVec(p,h,hndl):
rho = hndl.PropsSI("D","P",p,"H",h,"Water")
t = hndl.PropsSI('T','P',p,'H',h,'Water')
x = hndl.PropsSI('Q','P',p,'H',h,'Water')
eta = hndl.PropsSI('V','P',p,'H',h,'Water')
return rho,t,eta,x

h = 1e3*np.linspace(700,1500,len)
p = 1e6*np.linspace(2.8,3,len)

t1 = time.time()
(rho,t,eta,x) = PyDir(p,h,CPpy)
t2 = time.time()
print('%e'%((t2 - t1)))

t1 = time.time()
for i in range(1,len):
(rho[i],t[i],eta[i],x[i]) = PyDirn(p[i],h[i],CPpy)
t2 = time.time()
print('%e'%((t2 - t1)))

t1 = time.time()
(rho,t,eta,x) = PyDirVec(p,h,CPpy)
t2 = time.time()
print('%e'%((t2 - t1)))

5.214521e+00
5.149483e+00
4.670270e+00

What is the best way to do the same for best computational speed?

Peter Otten

unread,
Apr 25, 2015, 9:19:36 AM4/25/15
to pytho...@python.org
As your timings suggest the way you pass numpy arrays directly in PyDirVec()
is certainly the most idiomatic and also the fastest approach for a library
that supports numpy.

If that's not fast enough you probably need to look into the library itself
(which seems to be written in C++ according to the link
<http://www.coolprop.org/index.html> you didn't provide).

[Disclaimer: I haven't used CoolProp and know nothing about it]

0 new messages