File tree 4 files changed +162
-0
lines changed
4 files changed +162
-0
lines changed Original file line number Diff line number Diff line change
1
+ These tests demonstrate 3 different ways to allocate memory for the graph
2
+ viewer.
3
+
4
+ The methods:
5
+
6
+ 1. global
7
+ This method allocates a global memory pool that is used by all structs.
8
+ It is the fastest method and should be easy to clean up. You do have to
9
+ make sure that any pointers to the memory used by others is cleaned up.
10
+ 1. malloc
11
+ This methods does two mallocs for every iteration. It is slightly slower
12
+ (2x as slow), but won't require as much unfragmented memory. It is harder
13
+ to clean up this memory, as it requires an equal amount of free's.
14
+ 2. array
15
+ This method uses NSMutableArray's to store the necessary information. It is
16
+ by far the slowest (10x slower than global) but will make use of
17
+ Objective-C's garbage collection. This is the easiest way to go if it isn't
18
+ too slow. Looping and creating the arrays takes about 2 seconds for 800k
19
+ iterations. The question is if this significantly slows down the work.
20
+
21
+ Results:
22
+
23
+ global: 0.18 seconds
24
+ malloc: 0.39 seconds
25
+ array: 1.90 seconds
26
+
Original file line number Diff line number Diff line change
1
+ #include < stdlib.h>
2
+ #include < stdio.h>
3
+ #include < xlocale.h>
4
+ #include < stdarg.h>
5
+ #include < unistd.h>
6
+ #include < string.h>
7
+ #include < Cocoa/Cocoa.h>
8
+
9
+ int main () {
10
+ srandomdev ();
11
+
12
+ int i = 0 ; struct list* last;
13
+ int num = atoi (" 8000000" );
14
+
15
+ int size = 1000 ;
16
+ int totColumns = 10000 ;
17
+ int currentColumn = 0 ;
18
+
19
+ NSMutableArray * array = [NSMutableArray arrayWithCapacity: 100 *size];
20
+
21
+ for (i = 0 ; i < num; i++) {
22
+ int numColumns = i % 5 ;
23
+
24
+ NSMutableArray * arr = [NSMutableArray arrayWithCapacity: numColumns];
25
+ int j;
26
+ for (j = 0 ; j < numColumns; j++)
27
+ [arr addObject: @" Ha" ];
28
+ [array addObject: arr];
29
+ }
30
+
31
+ [array release ];
32
+ return 0 ;
33
+ }
Original file line number Diff line number Diff line change
1
+ #include <stdlib.h>
2
+ #include <stdio.h>
3
+ #include <xlocale.h>
4
+ #include <stdarg.h>
5
+ #include <unistd.h>
6
+ #include <string.h>
7
+ struct list {
8
+ void * columns ;
9
+ int numColumns ;
10
+ };
11
+ struct hash {
12
+ char value [40 ];
13
+ };
14
+
15
+ int main () {
16
+ srandomdev ();
17
+
18
+ int i = 0 ; struct list * last ;
19
+ int num = atoi ("8000000" );
20
+
21
+ int size = 1000 ;
22
+ int totColumns = 10000 ;
23
+ int currentColumn = 0 ;
24
+
25
+ /* Initialize initial list of revisions */
26
+ struct list * revisionList = malloc (size * sizeof (struct list ));
27
+ struct hash * columns = malloc (totColumns * sizeof (struct hash ));
28
+
29
+ struct hash standardColumn ;
30
+ strcpy (standardColumn .value , "Haha pieter" );
31
+ for (i = 0 ; i < num ; i ++ ) {
32
+ if (size <= i ) {
33
+ size *= 2 ;
34
+ revisionList = realloc (revisionList , size * sizeof (struct list ));
35
+ }
36
+
37
+ struct list * a = revisionList + i ;
38
+ a -> numColumns = i % 5 ;
39
+ if (currentColumn + a -> numColumns > totColumns ) {
40
+ totColumns *= 2 ;
41
+ printf ("Reallocing columns. New total: %i\n" , totColumns );
42
+ columns = realloc (columns , totColumns * sizeof (struct hash ));
43
+ }
44
+ int j ;
45
+ for (j = 0 ; j < a -> numColumns ; j ++ ) {
46
+ //ccolumns[currentColumn++] = st
47
+ strncpy (columns [currentColumn ++ ].value , "Haha pieter is cool" , 20 );
48
+ }
49
+ }
50
+
51
+ printf ("Num value at 3000 is: %i vs %i\n" , revisionList [3000 ].numColumns , (int ) (5 * random ()));
52
+ printf ("Value of 1000'd column is: %s\n" , columns [1000 ].value );
53
+ sleep (5 );
54
+ return 0 ;
55
+ }
Original file line number Diff line number Diff line change
1
+ #include <stdlib.h>
2
+ #include <stdio.h>
3
+ #include <xlocale.h>
4
+ #include <stdarg.h>
5
+ #include <unistd.h>
6
+ #include <string.h>
7
+
8
+ struct list {
9
+ struct hash * columns ;
10
+ int numColumns ;
11
+ };
12
+ struct hash {
13
+ char value [40 ];
14
+ };
15
+
16
+ int main () {
17
+ srandomdev ();
18
+
19
+ int i = 0 ; struct list * last ;
20
+ int num = atoi ("8000000" );
21
+
22
+ int size = 1000 ;
23
+ /* Initialize initial list of revisions */
24
+ struct list * * revisionList = malloc (size * sizeof (struct list * ));
25
+
26
+ struct hash standardColumn ;
27
+ strcpy (standardColumn .value , "Haha pieter" );
28
+ for (i = 0 ; i < num ; i ++ ) {
29
+ if (size <= i ) {
30
+ size *= 2 ;
31
+ revisionList = realloc (revisionList , size * sizeof (struct list * ));
32
+ }
33
+
34
+ struct list * a = malloc (sizeof (struct list ));
35
+ revisionList [i ] = a ;
36
+
37
+ a -> numColumns = i % 5 ;
38
+ a -> columns = malloc (a -> numColumns * sizeof (struct hash ));
39
+ int j ;
40
+ for (j = 0 ; j < a -> numColumns ; j ++ ) {
41
+ //ccolumns[currentColumn++] = st
42
+ strncpy (a -> columns [j ].value , "Haha pieter is cool" , 20 );
43
+ }
44
+ }
45
+
46
+ printf ("Num value at 3000 is: %i vs %i\n" , revisionList [3000 ]-> numColumns , (int ) (5 * random ()));
47
+ return 0 ;
48
+ }
You can’t perform that action at this time.
0 commit comments