Skip to content

Commit 9605de0

Browse files
committed
week 07 initial drop
1 parent 744d739 commit 9605de0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+3739
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import time
2+
import timer
3+
4+
x = 0
5+
6+
# @profile
7+
@timer.timer
8+
def slow():
9+
x = 0
10+
def doit1(i):
11+
global x
12+
x = x + i
13+
14+
list = range(100000)
15+
for i in list:
16+
doit1(i)
17+
18+
# @profile
19+
@timer.timer
20+
def fast():
21+
x = 0
22+
def doit2(list):
23+
global x
24+
for i in list:
25+
x = x + i
26+
list = range(100000)
27+
doit2(list)
28+
29+
if __name__ == "__main__":
30+
s1 = slow()
31+
s2 = fast()
32+
assert(s1 == s2)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python
2+
3+
from timer import timer
4+
5+
list_data = [l.strip() for l in open('/usr/share/dict/words')] * 10
6+
7+
set_data = set(list_data)
8+
9+
10+
@timer
11+
def set_contains(x):
12+
return x in set_data
13+
14+
@timer
15+
def list_contains(x):
16+
return x in list_data
17+
18+
19+
if __name__ == "__main__":
20+
set_contains("zebra")
21+
list_contains("zebra")
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python
2+
3+
from timer import timer
4+
5+
data = [l.strip() for l in open('/usr/share/dict/words')] * 10
6+
7+
# data contains a list of words. uppercase all elements two ways:
8+
9+
@timer
10+
def slow():
11+
upper = []
12+
for word in data:
13+
upper.append(word.upper())
14+
return upper
15+
16+
@timer
17+
def fast_list_comprehension():
18+
return [x.upper() for x in data]
19+
20+
@timer
21+
def fast_map():
22+
return map(str.upper, data)
23+
24+
@timer
25+
def fast_generator():
26+
# super fast. but is it doing the same work?
27+
return (x.upper() for x in data)
28+
29+
if __name__ == "__main__":
30+
slow()
31+
fast_map()
32+
fast_list_comprehension()
33+
fast_generator()
32 KB
Loading
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import time
2+
3+
def timer(func):
4+
def timer(*args, **kwargs):
5+
t1 = time.time()
6+
result = func(*args, **kwargs)
7+
t2 = time.time()
8+
print "-- executed %s in %.4f seconds" % (func.func_name, (t2 - t1))
9+
return result
10+
return timer
11+

Examples/week-07-profiling/django/benchmark/benchmark/__init__.py

Whitespace-only changes.
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# Django settings for benchmark project.
2+
import os
3+
4+
DEBUG = True
5+
TEMPLATE_DEBUG = DEBUG
6+
7+
ADMINS = (
8+
# ('Your Name', '[email protected]'),
9+
)
10+
11+
MANAGERS = ADMINS
12+
DIRNAME = os.path.dirname(os.path.abspath(__file__))
13+
14+
DATABASES = {
15+
'default': {
16+
'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
17+
'NAME': '', # Or path to database file if using sqlite3.
18+
# The following settings are not used with sqlite3:
19+
'USER': '',
20+
'PASSWORD': '',
21+
'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
22+
'PORT': '', # Set to empty string for default.
23+
}
24+
}
25+
26+
# Hosts/domain names that are valid for this site; required if DEBUG is False
27+
# See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts
28+
ALLOWED_HOSTS = []
29+
30+
# Local time zone for this installation. Choices can be found here:
31+
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
32+
# although not all choices may be available on all operating systems.
33+
# In a Windows environment this must be set to your system time zone.
34+
TIME_ZONE = 'America/Chicago'
35+
36+
# Language code for this installation. All choices can be found here:
37+
# http://www.i18nguy.com/unicode/language-identifiers.html
38+
LANGUAGE_CODE = 'en-us'
39+
40+
SITE_ID = 1
41+
42+
# If you set this to False, Django will make some optimizations so as not
43+
# to load the internationalization machinery.
44+
USE_I18N = True
45+
46+
# If you set this to False, Django will not format dates, numbers and
47+
# calendars according to the current locale.
48+
USE_L10N = True
49+
50+
# If you set this to False, Django will not use timezone-aware datetimes.
51+
USE_TZ = True
52+
53+
# Absolute filesystem path to the directory that will hold user-uploaded files.
54+
# Example: "/var/www/example.com/media/"
55+
MEDIA_ROOT = ''
56+
57+
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
58+
# trailing slash.
59+
# Examples: "http://example.com/media/", "http://media.example.com/"
60+
MEDIA_URL = ''
61+
62+
# Absolute path to the directory static files should be collected to.
63+
# Don't put anything in this directory yourself; store your static files
64+
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
65+
# Example: "/var/www/example.com/static/"
66+
STATIC_ROOT = ''
67+
68+
# URL prefix for static files.
69+
# Example: "http://example.com/static/", "http://static.example.com/"
70+
STATIC_URL = '/static/'
71+
72+
# Additional locations of static files
73+
STATICFILES_DIRS = (
74+
# Put strings here, like "/home/html/static" or "C:/www/django/static".
75+
# Always use forward slashes, even on Windows.
76+
# Don't forget to use absolute paths, not relative paths.
77+
)
78+
79+
# List of finder classes that know how to find static files in
80+
# various locations.
81+
STATICFILES_FINDERS = (
82+
'django.contrib.staticfiles.finders.FileSystemFinder',
83+
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
84+
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
85+
)
86+
87+
# Make this unique, and don't share it with anybody.
88+
SECRET_KEY = 'ls8e$xpgo_(4%(iov2%jv0%wh+%#+kgl+p7cwcu$4)+qh$wh8!'
89+
90+
# List of callables that know how to import templates from various sources.
91+
TEMPLATE_LOADERS = (
92+
'django.template.loaders.filesystem.Loader',
93+
'django.template.loaders.app_directories.Loader',
94+
# 'django.template.loaders.eggs.Loader',
95+
)
96+
97+
MIDDLEWARE_CLASSES = (
98+
'django.middleware.common.CommonMiddleware',
99+
'django.contrib.sessions.middleware.SessionMiddleware',
100+
'django.middleware.csrf.CsrfViewMiddleware',
101+
'django.contrib.auth.middleware.AuthenticationMiddleware',
102+
'django.contrib.messages.middleware.MessageMiddleware',
103+
# Uncomment the next line for simple clickjacking protection:
104+
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
105+
)
106+
107+
ROOT_URLCONF = 'benchmark.urls'
108+
109+
# Python dotted path to the WSGI application used by Django's runserver.
110+
WSGI_APPLICATION = 'benchmark.wsgi.application'
111+
112+
TEMPLATE_DIRS = (
113+
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
114+
# Always use forward slashes, even on Windows.
115+
# Don't forget to use absolute paths, not relative paths.
116+
os.path.join(DIRNAME, 'templates'),
117+
)
118+
119+
INSTALLED_APPS = (
120+
'django.contrib.auth',
121+
'django.contrib.contenttypes',
122+
'django.contrib.sessions',
123+
'django.contrib.sites',
124+
'django.contrib.messages',
125+
'django.contrib.staticfiles',
126+
'django.contrib.humanize',
127+
128+
129+
'benchmarkapp',
130+
# Uncomment the next line to enable the admin:
131+
# 'django.contrib.admin',
132+
# Uncomment the next line to enable admin documentation:
133+
# 'django.contrib.admindocs',
134+
)
135+
136+
# A sample logging configuration. The only tangible logging
137+
# performed by this configuration is to send an email to
138+
# the site admins on every HTTP 500 error when DEBUG=False.
139+
# See http://docs.djangoproject.com/en/dev/topics/logging for
140+
# more details on how to customize your logging configuration.
141+
LOGGING = {
142+
'version': 1,
143+
'disable_existing_loggers': False,
144+
'filters': {
145+
'require_debug_false': {
146+
'()': 'django.utils.log.RequireDebugFalse'
147+
}
148+
},
149+
'handlers': {
150+
'mail_admins': {
151+
'level': 'ERROR',
152+
'filters': ['require_debug_false'],
153+
'class': 'django.utils.log.AdminEmailHandler'
154+
}
155+
},
156+
'loggers': {
157+
'django.request': {
158+
'handlers': ['mail_admins'],
159+
'level': 'ERROR',
160+
'propagate': True,
161+
},
162+
}
163+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{% load humanize %}
2+
3+
<p>
4+
{{ data}}
5+
</p>
6+
7+
<p>
8+
Benchmark index
9+
</p>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from django.conf.urls import patterns, include, url
2+
3+
4+
# Uncomment the next two lines to enable the admin:
5+
# from django.contrib import admin
6+
# admin.autodiscover()
7+
8+
urlpatterns = patterns('',
9+
# Examples:
10+
url(r'^$', 'benchmarkapp.views.index', name='index'),
11+
# url(r'^benchmark/', include('benchmark.foo.urls')),
12+
13+
# Uncomment the admin/doc line below to enable admin documentation:
14+
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
15+
16+
# Uncomment the next line to enable the admin:
17+
# url(r'^admin/', include(admin.site.urls)),
18+
)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
WSGI config for benchmark project.
3+
4+
This module contains the WSGI application used by Django's development server
5+
and any production WSGI deployments. It should expose a module-level variable
6+
named ``application``. Django's ``runserver`` and ``runfcgi`` commands discover
7+
this application via the ``WSGI_APPLICATION`` setting.
8+
9+
Usually you will have the standard Django WSGI application here, but it also
10+
might make sense to replace the whole Django WSGI application with a custom one
11+
that later delegates to the Django one. For example, you could introduce WSGI
12+
middleware here, or combine a Django application with an application of another
13+
framework.
14+
15+
"""
16+
import os
17+
18+
# We defer to a DJANGO_SETTINGS_MODULE already in the environment. This breaks
19+
# if running multiple sites in the same mod_wsgi process. To fix this, use
20+
# mod_wsgi daemon mode with each site in its own daemon process, or use
21+
# os.environ["DJANGO_SETTINGS_MODULE"] = "benchmark.settings"
22+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "benchmark.settings")
23+
24+
# This application object is used by any WSGI server configured to use this
25+
# file. This includes Django's development server, if the WSGI_APPLICATION
26+
# setting points here.
27+
from django.core.wsgi import get_wsgi_application
28+
application = get_wsgi_application()
29+
30+
# Apply WSGI middleware here.
31+
# from helloworld.wsgi import HelloWorldApplication
32+
# application = HelloWorldApplication(application)

0 commit comments

Comments
 (0)