last tp
This commit is contained in:
parent
948d17166a
commit
ff6eeb2fad
72
17nov/displayCpu.py
Normal file
72
17nov/displayCpu.py
Normal file
@ -0,0 +1,72 @@
|
||||
"""Pour executer ce script, vous devez installer matplotlib"""
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
import itertools
|
||||
|
||||
|
||||
class CpuPlot(object):
|
||||
def __init__(self, n):
|
||||
"""
|
||||
Initialize an object that will be used to display data points on the
|
||||
screen.
|
||||
n -- An array of x-values.
|
||||
"""
|
||||
|
||||
self.n = n
|
||||
self.courbes = []
|
||||
self.labels = []
|
||||
|
||||
def prepare(self, data, label=None):
|
||||
"""
|
||||
Add a data points.
|
||||
"""
|
||||
|
||||
self.courbes.append(data)
|
||||
self.labels.append(label)
|
||||
|
||||
def reset(self):
|
||||
"""
|
||||
Reset data points. Note that x-values are keeped.
|
||||
"""
|
||||
|
||||
self.courbes = []
|
||||
|
||||
def draw(self):
|
||||
"""
|
||||
Draw the data points on the screen.
|
||||
"""
|
||||
|
||||
plt.xlim(max(0, min(self.n) - 5), max(self.n) + 5)
|
||||
plt.ylim(0, max([max(t) for t in self.courbes]) + 5)
|
||||
|
||||
plt.xlabel('input size')
|
||||
plt.ylabel('milliseconds')
|
||||
plt.title('CPU time charts')
|
||||
|
||||
color = itertools.cycle('bgrcmyk')
|
||||
|
||||
for i, t in enumerate(self.courbes):
|
||||
if self.labels[i] is None:
|
||||
plt.plot(self.n, t, '%s-o' % next(color),
|
||||
label='Data points %d' % i)
|
||||
else:
|
||||
plt.plot(self.n, t, '%s-o' % next(color),
|
||||
label=self.labels[i])
|
||||
|
||||
plt.legend()
|
||||
plt.show()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Create a CpuPlot object for x-values 10, 20, 30, 40
|
||||
afficheur = CpuPlot([10, 20, 30, 40])
|
||||
|
||||
# Add two sets of data points
|
||||
afficheur.prepare([1.1, 2.4, 2.8, 4.1], "Points 1")
|
||||
afficheur.prepare([1.2, 4.5, 8.4, 16.5])
|
||||
|
||||
# Display
|
||||
afficheur.draw()
|
||||
|
||||
# Don't exit too fast
|
||||
input("Press [Enter] to exit.")
|
25
17nov/ex5.py
Normal file
25
17nov/ex5.py
Normal file
@ -0,0 +1,25 @@
|
||||
|
||||
def bubble_sort(liste:list):
|
||||
"""Sort the list with bubble sort inline """
|
||||
Order = False
|
||||
while not Order:
|
||||
Order = True
|
||||
for i in range(len(liste)-1):
|
||||
if liste[i] > liste[i+1]:
|
||||
liste[i], liste[i+1] = liste[i+1], liste[i]
|
||||
Order = False
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from random import shuffle
|
||||
|
||||
from displayCpu import CpuPlot
|
||||
from umons_cpu import cpu_time
|
||||
from sort import dicho_search, insertion_sort, merge_sort, selection_sort
|
||||
|
||||
test_bubble = [cpu_time(bubble_sort, [i for i in range(j)]) for j in range(10)]
|
||||
|
||||
graph = CpuPlot(list(range(10)))
|
||||
graph.prepare(test_bubble)
|
||||
graph.draw()
|
||||
|
BIN
17nov/serie8.pdf
Normal file
BIN
17nov/serie8.pdf
Normal file
Binary file not shown.
107
17nov/sort.py
Normal file
107
17nov/sort.py
Normal file
@ -0,0 +1,107 @@
|
||||
"""
|
||||
Voir commentaires concernant ce module dans l'enonce de la serie de TP.
|
||||
"""
|
||||
|
||||
import random
|
||||
import umons_cpu
|
||||
|
||||
def python_sort(t):
|
||||
t.sort()
|
||||
|
||||
def insertion_sort(t):
|
||||
n = len(t)
|
||||
for i in range(1,n):
|
||||
clef = t[i]
|
||||
j = i - 1
|
||||
while j >= 0 and t[j] > clef:
|
||||
t[j+1] = t[j]
|
||||
j = j - 1
|
||||
t[j+1] = clef
|
||||
|
||||
def selection_sort(t):
|
||||
n = len(t)
|
||||
for i in range(n-1):
|
||||
small = i
|
||||
for j in range(i+1,n):
|
||||
if t[j] < t[small]:
|
||||
small = j
|
||||
(t[i], t[small]) = (t[small], t[i])
|
||||
|
||||
# permet d'avoir une interface in place, similaire aux autres tris
|
||||
def merge_sort(t):
|
||||
t[:] = merge_sort_functionnal(t)
|
||||
|
||||
def merge_sort_functionnal(t):
|
||||
n = len(t)
|
||||
if n > 1:
|
||||
(t1, t2) = split(t)
|
||||
t1 = merge_sort_functionnal(t1)
|
||||
t2 = merge_sort_functionnal(t2)
|
||||
return merge(t1, t2)
|
||||
else:
|
||||
return t
|
||||
|
||||
def split(t):
|
||||
""" precondition: len(t) >= 2 """
|
||||
mid = len(t) // 2
|
||||
t1 = t[:mid]
|
||||
t2 = t[mid:]
|
||||
return (t1, t2)
|
||||
|
||||
def merge(t1, t2):
|
||||
if len(t1) == 0:
|
||||
return t2
|
||||
elif len(t2) == 0:
|
||||
return t1
|
||||
elif t1[0] < t2[0]:
|
||||
return [t1[0]] + merge(t1[1:], t2)
|
||||
else:
|
||||
return [t2[0]] + merge(t1, t2[1:])
|
||||
|
||||
def dicho_search(t, x):
|
||||
start = 0
|
||||
end = len(t) - 1
|
||||
mid = start + (end - start) // 2
|
||||
while (end - start > 0) and x != t[mid]:
|
||||
if x < t[mid]:
|
||||
end = mid - 1
|
||||
else:
|
||||
start = mid + 1
|
||||
mid = start + (end - start) // 2
|
||||
if len(t) > 0 and x == t[mid]:
|
||||
return mid
|
||||
else:
|
||||
return None
|
||||
|
||||
def test(n):
|
||||
t1 = list(range(n))
|
||||
t2 = list(range(n,0,-1))
|
||||
t3 = []
|
||||
for i in range(n):
|
||||
t3.append(random.randint(0,n))
|
||||
print('%7d %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f' % (
|
||||
n,
|
||||
umons_cpu.cpu_time(selection_sort, t1),
|
||||
umons_cpu.cpu_time(insertion_sort, t1),
|
||||
umons_cpu.cpu_time(merge_sort, t1),
|
||||
umons_cpu.cpu_time(selection_sort, t2),
|
||||
umons_cpu.cpu_time(insertion_sort, t2),
|
||||
umons_cpu.cpu_time(merge_sort, t2),
|
||||
umons_cpu.cpu_time(selection_sort, t3),
|
||||
umons_cpu.cpu_time(insertion_sort, t3),
|
||||
umons_cpu.cpu_time(merge_sort, t3)))
|
||||
|
||||
if __name__ == '__main__':
|
||||
print('Temps affiches en msec')
|
||||
print(' n '
|
||||
't1: sel '
|
||||
' ins '
|
||||
' mer '
|
||||
't2: sel '
|
||||
' ins '
|
||||
' mer '
|
||||
't3: sel '
|
||||
' ins '
|
||||
' mer')
|
||||
for i in range(100, 901, 100):
|
||||
test(i)
|
109
17nov/umons_cpu.py
Normal file
109
17nov/umons_cpu.py
Normal file
@ -0,0 +1,109 @@
|
||||
"""
|
||||
Module utilisé à l'UMONS dans le cadre des cours de Programmation et
|
||||
Algorithmique 1 et Structure de Données 1 pour faciliter le calcul
|
||||
des temps CPU.
|
||||
|
||||
Auteur: Pierre Hauweele et Hadrien Mélot (Université de Mons), 2016
|
||||
"""
|
||||
|
||||
import timeit
|
||||
import pickle
|
||||
|
||||
|
||||
def __init_timer__(f, *args):
|
||||
fs = pickle.dumps(f)
|
||||
argss = pickle.dumps(args)
|
||||
setup = \
|
||||
"""
|
||||
import pickle
|
||||
import copy
|
||||
f = pickle.loads(%s)
|
||||
args = pickle.loads(%s)
|
||||
""" % (fs, argss)
|
||||
stmt = 'f(*copy.deepcopy(args))'
|
||||
return timeit.Timer(stmt, setup)
|
||||
|
||||
|
||||
def __calibrate__(t):
|
||||
calibrate_test = 0
|
||||
n = 1
|
||||
|
||||
while calibrate_test < 0.1:
|
||||
n *= 10
|
||||
calibrate_test = t.timeit(n)
|
||||
|
||||
return n, calibrate_test
|
||||
|
||||
|
||||
def cpu_time(f, *args):
|
||||
""" Retourne un temps CPU exprimé en millisecondes (ms)
|
||||
- f : fonction ou méthode à tester
|
||||
- *args : liste d'arguments pour f. Ces arguments ne sont pas
|
||||
modifiés, même si la fonction f a des effets de bord (ils sont
|
||||
copiés avant l'exécution).
|
||||
|
||||
Exemples :
|
||||
cputime(math.sqrt, 4)
|
||||
pour calculer le temps CPU de math.sqrt(4)
|
||||
cputime(str.upper, 'hello')
|
||||
pour calculer le temps CPU de 'hello'.upper()
|
||||
cputime(myfunc, x, y, z)
|
||||
pour calculer le temps CPU de myfunc(x, y, z)
|
||||
"""
|
||||
t = __init_timer__(f, *args)
|
||||
|
||||
n, cal_time = __calibrate__(t)
|
||||
|
||||
res = min([cal_time] + t.repeat(2, n))
|
||||
|
||||
return (res / n) * 1000
|
||||
|
||||
|
||||
def calibrate(f, *args):
|
||||
""" Retourne un nombre de tests qui rend le calcul du temps CPU
|
||||
a priori raisonnable.
|
||||
- f : fonction ou méthode à tester
|
||||
- *args : liste d'arguments pour f. Ces arguments ne sont pas
|
||||
modifiés, même si la fonction f a des effets de bord (ils sont
|
||||
copiés avant l'exécution).
|
||||
|
||||
Le nombre de tests retourné est une puissance de 10 (au minimum 10). Il
|
||||
sera d'autant plus grand si la fonction semble rapide.
|
||||
"""
|
||||
t = __init_timer__(f, *args)
|
||||
|
||||
n, cal_time = __calibrate__(t)
|
||||
|
||||
return n
|
||||
|
||||
|
||||
def cpu_time_without_copy(f, *args):
|
||||
""" Retourne un temps CPU exprimé en millisecondes (ms)
|
||||
- f : fonction ou méthode à tester
|
||||
- *args : liste d'arguments pour f.
|
||||
Cette version ne copie pas les arguments:
|
||||
il ne faut donc l'utiliser qu'avec des fonctions
|
||||
sans effet de bord !
|
||||
"""
|
||||
fs = pickle.dumps(f)
|
||||
argss = pickle.dumps(args)
|
||||
setup = \
|
||||
"""
|
||||
import pickle
|
||||
f = pickle.loads(%s)
|
||||
args = pickle.loads(%s)
|
||||
""" % (fs, argss)
|
||||
stmt = 'f(*args)'
|
||||
t = timeit.Timer(stmt, setup)
|
||||
|
||||
calibrate_test = 0
|
||||
n = 1
|
||||
|
||||
while calibrate_test < 0.1:
|
||||
n *= 10
|
||||
calibrate_test = t.timeit(n)
|
||||
|
||||
res = min([calibrate_test] + t.repeat(2, n))
|
||||
|
||||
return (res / n) * 1000
|
||||
|
BIN
renforcement/dhcp-4.4.3.P1-1-x86_64.pkg.tar.zst
Normal file
BIN
renforcement/dhcp-4.4.3.P1-1-x86_64.pkg.tar.zst
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user