Created
March 19, 2015 15:02
-
-
Save HelloZeroNet/b25b144506a3f8afbbda to your computer and use it in GitHub Desktop.
Simple plugin benchmark
This file contains 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 time | |
# No plugin (without getattr 0.056s, with getattr 0.110s) | |
class Request(object): | |
def __init__(self): | |
pass | |
def route(self, req): | |
func_name = "action"+req | |
if hasattr(self, func_name): | |
func = getattr(self, func_name) | |
return func() | |
def actionHello(self): | |
return "Hello!" | |
s = time.time() | |
request = Request() | |
for i in range(100000): | |
back = request.route("Hello") | |
print "- No plugin, 100 000 times in %.3fs (res: %s)" % (time.time()-s, back) | |
# Recreated | |
s = time.time() | |
for i in range(100000): | |
request = Request() | |
back = request.route("Hello") | |
print "- No plugin recreated, 100 000 times in %.3fs (res: %s)" % (time.time()-s, back) | |
# With 10 Plugin | |
class RequestPluginBye1: | |
def actionBye1(self): | |
return "Bye1!" | |
class RequestPluginBye2: | |
def actionBye2(self): | |
return "Bye2!" | |
class RequestPluginBye3: | |
def actionBye3(self): | |
return "Bye3!" | |
class RequestPluginBye4: | |
def actionBye4(self): | |
return "Bye4!" | |
class RequestPluginBye5: | |
def actionBye5(self): | |
return "Bye5!" | |
class RequestPluginBye6: | |
def actionBye6(self): | |
return "Bye6!" | |
class RequestPluginBye7: | |
def actionBye2(self): | |
return "Bye7!" | |
class RequestPluginBye8: | |
def actionBye8(self): | |
return "Bye8!" | |
class RequestPluginBye9: | |
def actionBye9(self): | |
return "Bye9!" | |
class RequestPluginBye10: | |
def actionBye10(self): | |
return "Bye10!" | |
class Request(RequestPluginBye1, RequestPluginBye2, RequestPluginBye3, RequestPluginBye4, RequestPluginBye5, RequestPluginBye6, RequestPluginBye7, RequestPluginBye8, RequestPluginBye9, RequestPluginBye10): | |
def __init__(self): | |
pass | |
def route(self, req): | |
func_name = "action"+req | |
if hasattr(self, func_name): | |
func = getattr(self, func_name) | |
return func() | |
def actionHello(self): | |
return "Hello!" | |
s = time.time() | |
request = Request() | |
for i in range(100000): | |
back = request.route("Bye10") | |
print "- 10 plugin, 100 000 times in %.3fs (res: %s)" % (time.time()-s, back) | |
# Recreated | |
s = time.time() | |
for i in range(100000): | |
request = Request() | |
back = request.route("Bye10") | |
print "- 10 plugin recreated, 100 000 times in %.3fs (res: %s)" % (time.time()-s, back) | |
# Type class | |
class Request(object): | |
def __init__(self): | |
pass | |
def route(self, req): | |
func_name = "action"+req | |
if hasattr(self, func_name): | |
func = getattr(self, func_name) | |
return func() | |
def actionHello(self): | |
return "Hello!" | |
plugins = [Request] | |
for i in range(1,11): | |
plugins.append(globals()["RequestPluginBye%s"%i]) | |
PluginedRequest = type("PluginedRequest", tuple(plugins), dict()) | |
s = time.time() | |
request = PluginedRequest() | |
for i in range(100000): | |
back = request.route("Bye10") | |
print "- 10 dynamic plugin, 100 000 times in %.3fs (res: %s)" % (time.time()-s, back) | |
# Recreated | |
PluginedRequest = type("PluginedRequest", tuple(plugins), dict()) | |
s = time.time() | |
for i in range(100000): | |
plugined_request = PluginedRequest() | |
back = plugined_request.route("Bye10") | |
print "- 10 dynamic plugin recreated, 100 000 times in %.3fs (res: %s)" % (time.time()-s, back) | |
""" | |
- No plugin, 100 000 times in 0.100s (res: Hello!) | |
- No plugin recreated, 100 000 times in 0.145s (res: Hello!) | |
- 10 plugin, 100 000 times in 0.211s (res: Bye10!) | |
- 10 plugin recreated, 100 000 times in 0.290s (res: Bye10!) | |
- 10 dynamic plugin, 100 000 times in 0.188s (res: Bye10!) | |
- 10 dynamic plugin recreated, 100 000 times in 0.234s (res: Bye10!) | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment