generated from carpentries/workbench-template-md
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Episode: Function Level Profiling (#6)
Closes #3
- Loading branch information
Showing
10 changed files
with
800 additions
and
6 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import time | ||
|
||
""" | ||
This is a synthetic program intended to produce a clear profile with cProfile/snakeviz | ||
Method names, constructed from a hex digit and a number clearly denote their position in the hierarchy. | ||
""" | ||
def a_1(): | ||
for i in range(3): | ||
b_1() | ||
time.sleep(1) | ||
b_2() | ||
|
||
|
||
def b_1(): | ||
c_1() | ||
c_2() | ||
|
||
def b_2(): | ||
time.sleep(1) | ||
|
||
def c_1(): | ||
time.sleep(0.5) | ||
|
||
|
||
def c_2(): | ||
time.sleep(0.3) | ||
d_1() | ||
|
||
def d_1(): | ||
time.sleep(0.1) | ||
|
||
|
||
|
||
|
||
# Entry Point | ||
a_1() |
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
""" | ||
Naive brute force travelling salesperson | ||
python travellingsales.py <cities> | ||
""" | ||
|
||
import itertools | ||
import math | ||
import random | ||
import sys | ||
|
||
def distance(point1, point2): | ||
return math.sqrt((point2[0] - point1[0])**2 + (point2[1] - point1[1])**2) | ||
|
||
def total_distance(points, order): | ||
total = 0 | ||
for i in range(len(order) - 1): | ||
total += distance(points[order[i]], points[order[i + 1]]) | ||
return total + distance(points[order[-1]], points[order[0]]) | ||
|
||
def traveling_salesman_brute_force(points): | ||
min_distance = float('inf') | ||
min_path = None | ||
for order in itertools.permutations(range(len(points))): | ||
d = total_distance(points, order) | ||
if d < min_distance: | ||
min_distance = d | ||
min_path = order | ||
return min_path, min_distance | ||
|
||
# Argument parsing | ||
if len(sys.argv) != 2: | ||
print("Script expects 1 positive integer argument, %u found."%(len(sys.argv) - 1)) | ||
sys.exit() | ||
cities_len = int(sys.argv[1]) | ||
if cities_len < 1: | ||
print("Script expects 1 positive integer argument, %s converts < 1."%(sys.argv[1])) | ||
sys.exit() | ||
# Define the cities as (x, y) coordinates | ||
random.seed(12) # Fixed random for consistency | ||
cities = [(0,0)] | ||
for i in range(cities_len): | ||
cities.append((random.uniform(-1, 1), random.uniform(-1, 1))) | ||
|
||
# Find the shortest path | ||
shortest_path, min_distance = traveling_salesman_brute_force(cities) | ||
print("Cities:", cities_len) | ||
print("Shortest Path:", shortest_path) | ||
print("Shortest Distance:", min_distance) |
Large diffs are not rendered by default.
Oops, something went wrong.
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
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