Best way to do

Peter Otten __peter__ at web.de
Sat Apr 25 15:19:11 CEST 2015


pauld11718 wrote:

> 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?

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]




More information about the Python-list mailing list