Benchmark
 All Classes Files Functions Variables Typedefs Macros
Macros | Functions
alloc.c File Reference

Comparaison entre l'allocation d'un tableau sur le heap et sur la stack. More...

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "benchmark.h"

Macros

#define N   100
 
#define SIZE_1   0x1000
 
#define SIZE_2   0x10000
 
#define SIZE_3   0x100000
 

Functions

char stack_1 ()
 allocation d'un tableau de taille SIZE_1 sur la stack More...
 
char stack_2 ()
 allocation d'un tableau de taille SIZE_2 sur la stack More...
 
char stack_3 ()
 allocation d'un tableau de taille SIZE_3 sur la stack More...
 
char heap_1 ()
 allocation d'un tableau de taille SIZE_1 sur le heap More...
 
char heap_2 ()
 allocation d'un tableau de taille SIZE_1 sur le heap More...
 
char heap_3 ()
 allocation d'un tableau de taille SIZE_1 sur le heap More...
 
void benchmark_fun (timer *t, char(*fun)(), recorder *rec, int x)
 Mesure N appels de fun More...
 
int main (int argc, char *argv[])
 

Detailed Description

Comparaison entre l'allocation d'un tableau sur le heap et sur la stack.

Macro Definition Documentation

#define N   100
#define SIZE_1   0x1000
#define SIZE_2   0x10000
#define SIZE_3   0x100000

Function Documentation

void benchmark_fun ( timer t,
char(*)()  fun,
recorder rec,
int  x 
)

Mesure N appels de fun

Parameters
ttimer utilisé pour la mesure du temps
funfonction dont on mesure les performances
recrecorder dans lequel on enregistre le temps
xabscisse à laquel on enregistre le temps
85  {
86  int i;
87  start_timer(t);
88  for (i = 0; i < N; i++) {
89  char c = fun();
90  }
91  write_record_n(rec, x, stop_timer(t), N);
92 }
void write_record_n(recorder *rec, long int x, long int time, long n)
Comme write_record mais divise time par n après avoir retiré l'overhead
Definition: benchmark.c:253
#define N
Definition: alloc.c:12
int i
Definition: mutsem.c:43
void start_timer(timer *t)
Stoque le temps actuel comme début de la mesure dans t
Definition: benchmark.c:87
long int stop_timer(timer *t)
Retourne le temps en nanosecondes depuis le début de mesure dans t
Definition: benchmark.c:133
char heap_1 ( )

allocation d'un tableau de taille SIZE_1 sur le heap

Le free a été commenté pour voir l'effet du remplissage du heap. Décommentez le pour voir ce que ça change.

50  {
51  char *s = (char *) malloc(sizeof(char) * SIZE_1);
52  s[SIZE_1 / 2] = 0;
53  return s[0];
54  //free(s);
55 }
#define SIZE_1
Definition: alloc.c:13
char heap_2 ( )

allocation d'un tableau de taille SIZE_1 sur le heap

60  {
61  char *s = (char *) malloc(sizeof(char) * SIZE_2);
62  s[SIZE_2 / 2] = 0;
63  return s[0];
64  //free(s);
65 }
#define SIZE_2
Definition: alloc.c:14
char heap_3 ( )

allocation d'un tableau de taille SIZE_1 sur le heap

70  {
71  char *s = (char *) malloc(sizeof(char) * SIZE_3);
72  s[SIZE_3 / 2] = 0;
73  return s[0];
74  //free(s);
75 }
#define SIZE_3
Definition: alloc.c:15
int main ( int  argc,
char *  argv[] 
)
94  {
95  timer *t = timer_alloc();
96 
97  // brk/sbrk
98  recorder *stack_rec = recorder_alloc("stack.csv");
99  recorder *heap_rec = recorder_alloc("heap.csv");
100 
101  benchmark_fun(t, heap_1, heap_rec, SIZE_1);
102  benchmark_fun(t, heap_2, heap_rec, SIZE_2);
103  benchmark_fun(t, heap_3, heap_rec, SIZE_3);
104 
105  benchmark_fun(t, stack_1, stack_rec, SIZE_1);
106  benchmark_fun(t, stack_2, stack_rec, SIZE_2);
107  benchmark_fun(t, stack_3, stack_rec, SIZE_3);
108 
109  printf("%p\n", sbrk(0));
110 
111  recorder_free(stack_rec);
112  recorder_free(heap_rec);
113 
114  timer_free(t);
115  return EXIT_SUCCESS;
116 }
void timer_free(timer *t)
Retourne le temps en nanosecondes depuis le début de mesure dans t
Definition: benchmark.c:172
char heap_3()
allocation d'un tableau de taille SIZE_1 sur le heap
Definition: alloc.c:70
recorder écrit les temps dans un fichier .csv
Definition: benchmark.c:208
char stack_2()
allocation d'un tableau de taille SIZE_2 sur la stack
Definition: alloc.c:29
timer permet de mesurer le temps écoulé entre deux moments
Definition: benchmark.c:43
timer * t
Definition: memfork.c:25
char stack_1()
allocation d'un tableau de taille SIZE_1 sur la stack
Definition: alloc.c:20
#define SIZE_2
Definition: alloc.c:14
recorder * recorder_alloc(char *filename)
Alloue un recorder
Definition: benchmark.c:219
#define SIZE_3
Definition: alloc.c:15
char stack_3()
allocation d'un tableau de taille SIZE_3 sur la stack
Definition: alloc.c:38
void recorder_free(recorder *rec)
Libère toutes les resources utilisées par rec
Definition: benchmark.c:263
timer * timer_alloc()
Alloue un timer
Definition: benchmark.c:63
void benchmark_fun(timer *t, char(*fun)(), recorder *rec, int x)
Mesure N appels de fun
Definition: alloc.c:85
#define SIZE_1
Definition: alloc.c:13
char heap_1()
allocation d'un tableau de taille SIZE_1 sur le heap
Definition: alloc.c:50
char heap_2()
allocation d'un tableau de taille SIZE_1 sur le heap
Definition: alloc.c:60
char stack_1 ( )

allocation d'un tableau de taille SIZE_1 sur la stack

20  {
21  char s[SIZE_1];
22  s[SIZE_1 / 2] = 0;
23  return s[0];
24 }
#define SIZE_1
Definition: alloc.c:13
char stack_2 ( )

allocation d'un tableau de taille SIZE_2 sur la stack

29  {
30  char s[SIZE_2];
31  s[SIZE_2 / 2] = 0;
32  return s[0];
33 }
#define SIZE_2
Definition: alloc.c:14
char stack_3 ( )

allocation d'un tableau de taille SIZE_3 sur la stack

38  {
39  char s[SIZE_3];
40  s[SIZE_3 / 2] = 0;
41  return s[0];
42 }
#define SIZE_3
Definition: alloc.c:15