Commits

Frederic De Groef committed 584290a

doing some profiling. added pstats2png bat script

  • Participants
  • Parent commits 482f31c

Comments (0)

Files changed (5)

src/demo_cython.py

 gradients2D = [(m.cos(i * TAU / N), m.sin(i * TAU / N)) for i in range(N)]
 
 
-if __name__ == '__main__':
-    w, h = 1024,1024
+def main():
+    w, h = 512,512
     n = 8
 
     time_before = time.time()
     texture =  noise.make_turbulence_texture(w,h, n, p, gradients2D)
     print "Time elapsed : %f seconds [size=(%d, %d), fractal=%d]" % (time.time() - time_before, w, h, n)
-    
+
 
     #normalize & show
     texture/=texture.max()
     plt.imshow(texture)#, cmap=plt.cm.gray)
     plt.colorbar()
     plt.show()
+    
+
+if __name__ == '__main__':
+    import sys
+    if len(sys.argv) == 3 and sys.argv[1] == "profile":
+        import pstats, cProfile
+        import pyximport
+        pyximport.install()
+        command = """main()"""
+        cProfile.runctx( command, globals(), locals(), filename="demo_cython_%s.profile" % sys.argv[2])
+    else:
+        main()
+
+
+

src/demo_matplotlib.py

 import time
 
 
-if __name__ == '__main__':
+def main():
     w, h = 512,512
     n = 8
 
 
     print "Time elapsed : %f seconds [size=(%d, %d), fractal=%d]" % (time.time() - time_before, w, h, n)
     values = noise_values
-    #normalize & show 
+    #normalize & show
     values/=values.max()
     plt.imshow(values)#, cmap=plt.cm.gray)
     plt.colorbar()
     plt.show()
 
-n
+
+if __name__ == '__main__':
+    import cProfile
+    command = """main()"""
+    cProfile.runctx( command, globals(), locals(), filename="demo_matplotlib.profile" )

src/make_graph.bat

+@ECHO off
+ECHO Generating graph for %1%
+
+@SET STATSFILE=%1%.profile
+@SET DOTFILE=%1%.dot
+@SET OUTFILE=%1%.png
+
+gprof2dot -f pstats -o %DOTFILE%  %STATSFILE%
+dot -Tpng -o %OUTFILE% %DOTFILE%
+del %DOTFILE%
+# encoding: utf-8
+# cython: profile=True
+
 from __future__ import division
 import numpy as np
 cimport numpy as np
-import cython
+cimport cython
+
 
 cdef extern from "math.h":
     cdef double floor(double)
     cdef double cos(double)
     cdef double sin(double)
-    cdef double abs(double)
+    cdef double fabs(double)
 
 
 
     cdef int idx = p[idx1] % 8
     return gradients2D[idx]
 
-cpdef double lerp(double a, double b, double alpha):
-    return a*(1-alpha) + b*(alpha)
 
-cpdef double dot(v1, v2):
+cdef double lerp(double a, double b, double alpha):
+    return a*(1.0-alpha) + b*(alpha)
+
+
+
+cdef double dot(v1, v2):
     return v1[0]*v2[0] + v1[1]*v2[1]
 
 
-cpdef double f(double t):
+
+cdef double f(double t):
     return (t * ( t * ( t * (6.0*t**2 - 15.0*t + 10.0))))
 
 
     return nxy
 
 
+
 def make_turbulence2D(double x, double y, int n_octaves, double lacunarity, double gain, np.ndarray[int] p, gradients2D):
     cdef double sum = 0.0
     cdef double freq = 1.0
     cdef ampl = 0.5
     cdef int i
     for i in range(n_octaves):
-        sum += noise2D(x*freq, y*freq, p, gradients2D) * ampl
+        sum += fabs(noise2D(x*freq, y*freq, p, gradients2D)) * ampl
         freq *= lacunarity  # 2.0**i
         ampl *= gain         # 0.5**
     return sum
 
 
 
-
-
 def make_turbulence_texture(int w, int h, int n_octaves,  np.ndarray[int] p, gradients2D):
     cdef np.ndarray[double, ndim=2] out = np.zeros((w,h))
     cdef int i, j
     for i in range(w-1):
         for j in range(h-1):
             x, y = i/(w - 1), j/(h - 1)
-            #print x, y
             val = make_turbulence2D(x, y, n_octaves, 2.0, 0.5, p, gradients2D)
-            #print val
             out[i, j] = val
     return out
 from Cython.Distutils import build_ext
 import numpy as np
 
-ext_modules = [Extension("noise_cy", ["noise_cy.pyx"],  include_dirs = [np.get_include()])]
+ext_modules = [Extension("noise_cy", ["noise_cy.pyx"], libraries=["m"], include_dirs = [np.get_include()])]
 
 setup(
   name = 'perlin_noise',