-
Notifications
You must be signed in to change notification settings - Fork 25
/
benchmark_Scale.py
executable file
·127 lines (108 loc) · 4.24 KB
/
benchmark_Scale.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python3
# Copyright © 2012-13 Qtrac Ltd. All rights reserved.
# This program or module is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version. It is provided for
# educational purposes and is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
import argparse
import cProfile
import os
import sys
import tempfile
import time
import Image
import Scale
try:
import cyImage
except ImportError:
cyImage = None
SHOW_SAVE_TIME = False
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-P", "--noprofile", action="store_true",
help="only use inside a regression.py file")
parser.add_argument("image", nargs="?", default=os.path.join(
os.path.dirname(__file__), "regressiondata/photo.xpm"),
help="image filename [default %(default)s]")
args = parser.parse_args()
profile = not args.noprofile
if SHOW_SAVE_TIME:
print("Loading", args.image, end="")
start = time.time()
image = Image.Image.from_file(args.image)
end = time.time() - start
if SHOW_SAVE_TIME:
print("\rLoaded with Image in {:.3f} sec{}".format(end,
" " * len(args.image)))
if cyImage is not None:
if SHOW_SAVE_TIME:
print("Loading", args.image, end="")
start = time.time()
image1 = cyImage.Image.from_file(args.image)
end = time.time() - start
if SHOW_SAVE_TIME:
print("\rLoaded with cyImage in {:.3f} sec{}".format(end,
" " * len(args.image)))
if profile:
print("Scaling using Image.scale()...")
cProfile.runctx("image.scale(0.5)", globals(), locals())
if cyImage is not None:
print("Scaling using cyImage.scale()...")
cProfile.runctx("image1.scale(0.5)", globals(), locals())
filenames = []
for name, function in (("Scale.scale_slow", Scale.scale_slow),
("Scale.scale_fast", Scale.scale_fast)):
scale(image, name, profile)
filenames.append((os.path.splitext(name)[1][1:], function))
if profile and SHOW_SAVE_TIME:
print("Saving rescaled images...")
filename = os.path.join(tempfile.gettempdir(), "scale.xpm")
start = time.time()
scaled = image.scale(0.5)
end = time.time() - start
if not profile:
print("Scaled with Image.scale() in {:.3f} sec".format(end))
if SHOW_SAVE_TIME:
start = time.time()
scaled.save(filename)
end = time.time() - start
print("Saved {} with Image.save() in {:.3f} sec".format(filename,
end))
if cyImage is not None:
start = time.time()
scaled1 = image1.scale(0.5)
end = time.time() - start
if not profile:
print("Scaled with cyImage.scale() in {:.3f} sec".format(end))
if SHOW_SAVE_TIME:
start = time.time()
scaled1.save(filename)
end = time.time() - start
print("Saved {} with cyImage.save() in {:.3f} sec".format(
filename, end))
for filename, function in filenames:
save(image, filename, function, profile)
def scale(image, function, profile):
if profile:
print("Scaling using {}()...".format(function))
cProfile.runctx("{}(image.pixels, image.width, "
"image.height, 0.5)".format(function), globals(), locals())
def save(image, name, function, profile):
filename = os.path.join(tempfile.gettempdir(), name + ".xpm")
start = time.time()
width, pixels = function(image.pixels, image.width, image.height, 0.5)
end = time.time() - start
if not profile:
print("Scaled with {}() in {:.3f} sec".format(name, end))
if SHOW_SAVE_TIME:
image = Image.Image.from_data(width, pixels)
start = time.time()
image.save(filename)
end = time.time() - start
print("Saved {} in {:.3f} sec".format(filename, end))
if __name__ == "__main__":
main()