| 1 |
#!/usr/bin/env python |
| 2 |
# |
| 3 |
# Copyright 2015 Google Inc. All Rights Reserved. |
| 4 |
# |
| 5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
# you may not use this file except in compliance with the License. |
| 7 |
# You may obtain a copy of the License at |
| 8 |
# |
| 9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
# |
| 11 |
# Unless required by applicable law or agreed to in writing, software |
| 12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
# See the License for the specific language governing permissions and |
| 15 |
# limitations under the License. |
| 16 |
|
| 17 |
import argparse |
| 18 |
import os |
| 19 |
import sys |
| 20 |
import time |
| 21 |
import numpy as np |
| 22 |
from subprocess import call |
| 23 |
|
| 24 |
COMMAND_TEMPLATE = "python ../compreffor/pyCompressor.py /path/to/font.otf --chunkratio %f" |
| 25 |
|
| 26 |
def time_run(value): |
| 27 |
command = COMMAND_TEMPLATE % value |
| 28 |
|
| 29 |
null = open(os.devnull, "wb") |
| 30 |
|
| 31 |
start_time = time.time() |
| 32 |
call(command.split(), stdout=null) |
| 33 |
run_time = time.time() - start_time |
| 34 |
|
| 35 |
return run_time |
| 36 |
|
| 37 |
def minimize_runtime(start_val, stop_val, samples, passes): |
| 38 |
left = start_val |
| 39 |
right = stop_val |
| 40 |
step = float(right - left) / samples |
| 41 |
|
| 42 |
for i in range(passes): |
| 43 |
print "Testing range (%f, %f) with %f steps" % (left, right, step) |
| 44 |
values = np.arange(left, right + step, step) |
| 45 |
times = map(time_run, values) |
| 46 |
|
| 47 |
lowest = min(enumerate(times), key=lambda x: x[1]) |
| 48 |
low_val, low_time = values[lowest[0]], lowest[1] |
| 49 |
print "Current lowest: %f with %gs" % (low_val, low_time) |
| 50 |
left = low_val - 2 * step |
| 51 |
while left <= 0: left += step |
| 52 |
right = low_val + 2 * step |
| 53 |
step = float(right - left) / samples |
| 54 |
|
| 55 |
return low_val |
| 56 |
|
| 57 |
def plot_values(start_val, stop_val, step): |
| 58 |
from matplotlib import pyplot |
| 59 |
|
| 60 |
values = np.arange(start_val, stop_val + step, step) |
| 61 |
times = map(time_run, values) |
| 62 |
pyplot.plot(values, times) |
| 63 |
pyplot.title("Time to run vs. Changing parameter") |
| 64 |
pyplot.xlabel("Parameter Value") |
| 65 |
pyplot.ylabel("Time to run (seconds)") |
| 66 |
pyplot.show() |
| 67 |
|
| 68 |
|
| 69 |
if __name__ == "__main__": |
| 70 |
parser = argparse.ArgumentParser(description='Minimize runtime.') |
| 71 |
parser.add_argument('-p', help='Plot values', action='store_true', default=False, |
| 72 |
required=False, dest="plot") |
| 73 |
parser.add_argument('start_val', help='What value to start at', type=float) |
| 74 |
parser.add_argument('stop_val', help='What value to stop at', type=float) |
| 75 |
parser.add_argument('step', help='What value to step by (for plotter) or number of samples (for minimizer)', type=float) |
| 76 |
parser.add_argument('passes', help='How many passes to run (for minimizer)', type=int, |
| 77 |
nargs="?") |
| 78 |
|
| 79 |
args = parser.parse_args() |
| 80 |
|
| 81 |
if not args.plot: |
| 82 |
assert args.passes != None, "passes argument required" |
| 83 |
min_val = minimize_runtime(args.start_val, args.stop_val, args.step, args.passes) |
| 84 |
print "Minimized time value: %f" % min_val |
| 85 |
else: |
| 86 |
plot_values(args.start_val, args.stop_val, args.step) |