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
|
#!/usr/bin/env python
#coding=utf-8
import random
def cre_files(nmega,nom_f1,nom_f2):
""" Création des fichiers d'essai, le paramètre est la taille en mega Octet"""
nlignes=nmega*15
with open(nom_f1,"w") as fo1:
fo1.write("")
i=1
for n1 in range(nlignes):
a=""
for n2 in range(10000):
if random.randint(0,10)<7:
a+=str(i)+" "+str(random.randint(0,10)/10.0)+"\n"
i+=1
with open(nom_f1,"a") as fo1:
fo1.write(a)
print"fin1"
with open(nom_f2,"w") as fo1:
fo1.write("")
i=1
for n1 in range(nlignes):
a=""
for n2 in range(10000):
if random.randint(0,10)<7:
a+=str(i)+" "+str(random.randint(0,10)/10.0)+"\n"
i+=1
with open(nom_f2,"a") as fo1:
fo1.write(a)
print"fin2"
def cre_bilan(nom_f1,nom_f2,nom_fs,nom_fm):
""" Création du bilan
nom_f1,nom_f2 : fichiers d'entrée
nom_fs : fichier de sortie, calcul f1-f2 si num ligne f1 num ligne f2
nom_fm : fichier de sortie, médianes sur 100 valeurs de fs
"""
with open(nom_f1,"r") as f1,open(nom_f2,"r") as f2,\
open(nom_fs,"w") as fs,open(nom_fm,"w") as fm:
ligne_f2=f2.readline()
num_l2=int(ligne_f2.split(" ")[0])
valeurs_med=[]
for ligne_f1 in f1: # Parcour du fichier f1
num_l1=int(ligne_f1.split(" ")[0])
while num_l1>=num_l2 and ligne_f2!="" : # Parcours de f2
if num_l2==num_l1: # Si égalite
difference=float(ligne_f1.split(" ")[1])-float(ligne_f2.split(" ")[1])
# rajout au fichier de sortie
fs.write(str(num_l2)+" "+str(difference)+"\n")
# Traitement des médianes
valeurs_med.append(difference)
if len(valeurs_med)==100 :
fm.write(str(sum(valeurs_med)/100)+"\n")
valeurs_med=[]
ligne_f2=f2.readline()
if ligne_f2!="": # Si la fin du fichier f2 n'est pas atteinte
num_l2=int(ligne_f2.split(" ")[0])
print "fin bilan"
nom_f1="f1.txt"
nom_f2="f2.txt"
cre_files(1,nom_f1,nom_f2) # 1=taille en Mo du fichier d'essai
nom_fs="fs.txt"
nom_fm="fm.txt"
cre_bilan(nom_f1,nom_f2,nom_fs,nom_fm) |
Partager