Created
December 4, 2013 11:02
-
-
Save cpelley/7785812 to your computer and use it in GitHub Desktop.
Partial datetime benchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import timeit | |
import numpy as np | |
import iris | |
from iris.pdatetime import PartialDateTime | |
def _ret_cube(): | |
cube = iris.cube.Cube(np.arange(4)) | |
time = iris.coords.DimCoord( | |
np.arange(4) * 4320, "time", | |
units=iris.unit.Unit("hours since epoch", calendar='360_day')) | |
cube.add_dim_coord(time, 0) | |
return cube | |
def using_partial(): | |
cube = _ret_cube() | |
# Providing the new partial datetime object. | |
constraint = iris.Constraint(time=PartialDateTime(year=1971, month=1)) | |
cube = cube.extract(constraint) | |
def using_numeric(): | |
cube = _ret_cube() | |
# Providing traditional numeric value as constraint. | |
constraint = iris.Constraint(time=8640) | |
cube = cube.extract(constraint) | |
if __name__ == '__main__': | |
t_partial = timeit.Timer("using_partial()", | |
"from __main__ import using_partial") | |
ptime = t_partial.repeat(1, 1000)[0] | |
print 'After: {}'.format(ptime) | |
t_numeric = timeit.Timer("using_numeric()", | |
"from __main__ import using_numeric") | |
ntime = t_numeric.repeat(1, 1000)[0] | |
print 'Before: {}'.format(ntime) | |
print '{}% increase'.format(((ptime - ntime) / ntime) * 100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment