Skip to content

Instantly share code, notes, and snippets.

Created September 9, 2014 09:29
Show Gist options
  • Save anonymous/32c8599466dad30cd551 to your computer and use it in GitHub Desktop.
Save anonymous/32c8599466dad30cd551 to your computer and use it in GitHub Desktop.
Benchmark for the function mapcoordinates (scipy).
# -*- coding: utf-8 -*-
"""
Benchmark for spline interpolation of a 3D wind field, using the function map_coordinates.
The spline interpolation is about 46000 times slower than a linear interpolation.
It is also about 10000 times slower than an equivalent program, written in the programming
language Julia.
"""
import time
import numpy as np
from scipy import ndimage
class Timer(object):
def __init__(self, verbose=False):
self.verbose = verbose
def __enter__(self):
self.start = time.time()
return self
def __exit__(self, *args):
self.end = time.time()
self.secs = self.end - self.start
self.msecs = self.secs * 1000 # millisecs
if self.verbose:
print 'elapsed time: %f ms' % self.msecs
def benchMapCoordinates(u, v, w, x, y, z, order = 2):
x_wind = ndimage.map_coordinates(u, [[x], [y], [z]], order=order)
y_wind = ndimage.map_coordinates(v, [[x], [y], [z]], order=order)
z_wind = ndimage.map_coordinates(w, [[x], [y], [z]], order=order)
return x_wind[0], y_wind[0], z_wind[0]
if __name__ == "__main__":
u = np.random.rand(2026, 51, 251)
v = np.random.rand(2026, 51, 251)
w = np.random.rand(2026, 51, 251)
with Timer() as t1:
benchMapCoordinates(u, v, w, 1, 2, 3, order=1)
print "time for benchMapCoordinates order = 1 [ms]: ", t1.secs * 1e3
with Timer() as t1:
benchMapCoordinates(u, v, w, 1, 2, 3, order=2)
print "time for benchMapCoordinates order = 2 [ms]: ", t1.secs * 1e3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment