/* * Dave's ScriptPerf in C with GLib * * To compile, use: * gcc -o scriptperf `glib-config --cflags` scriptperf.c `glib-config --libs` * * Author: Brandon Long * Date: 2001-04-19 * */ #include #include #include #include #include double timef (void) { double f = 0; struct timeval tv; int ret; ret = gettimeofday(&tv, NULL); if (ret == 0) { f = tv.tv_sec + (tv.tv_usec / 1000000.0); } return f; } void index_test (int INDEX) { GHashTable *hash; int x, b; hash = g_hash_table_new (g_int_hash, g_int_equal); for (x = 0; x < INDEX; x++) { g_hash_table_insert (hash, &x, &x); } for (x = 0; x < INDEX; x++) { b = (int) g_hash_table_lookup (hash, &x); } g_hash_table_destroy (hash); } void index_string_test (int INDEX) { GHashTable *hash; int x, b; hash = g_hash_table_new (g_str_hash, g_str_equal); for (x = 0; x < INDEX+0; x++) { g_hash_table_insert (hash, "some_string", &x); } for (x = 1; x < INDEX+1; x++) { b = (int) g_hash_table_lookup (hash, "some_string"); } g_hash_table_destroy (hash); } void loop_test (int INDEX) { int a, b; for (a = 0; a < INDEX; a++) for (b = 0; b < 10; b++) b = b; } void nested_int_test (int NESTED) { int n, a, b, c, d, e; n = 0; for (a = 0; a < NESTED; a++) { n = n + 34; for (b = 0; b < NESTED; b++) { n = n + 9; for (c = 0; c < NESTED; c++) { n = n + 5; for (d = 0; d < NESTED; d++) { n = n + 3; for (e = 0; e < NESTED; e++) { n = n - 100; } } } } } } int dothisB(int a, int b) { return a; } void returntest (int count) { int i, a; for (i = 0; i < count; i++) { a = dothisB(1,2); } } int dothis(int a) { return a; } void functest(int count) { int i; for (i = 0; i < count; i++) { dothis(10); } } typedef struct _Foo { int (*dothis)(int); } Foo; int Foo_dothis(int a) { return a; } void objecttest(int count) { Foo obj = {Foo_dothis}; int i; for (i = 0; i < count; i++) { obj.dothis(10); } } void file_line_test (int NESTED) { FILE *fp; char buf[256]; fp = fopen("testfile.dat", "r"); while (fgets(buf, sizeof(buf), fp)); fclose(fp); } void file_line_split_test (int NESTED) { FILE *fp; char buf[256]; gchar **line_parts; fp = fopen("testfile.dat", "r"); while (fgets(buf, sizeof(buf), fp)) { line_parts = g_strsplit (buf, " ", 0); g_strfreev(line_parts); } fclose(fp); } void list_append_test (int count) { GArray *garray; int i; garray = g_array_new (FALSE, FALSE, sizeof(gint)); for (i = 0; i < count; i++) { g_array_append_val (garray, i); } g_array_free (garray, TRUE); } void list_traverse_test (int count) { GArray *garray; int i, x; garray = g_array_new (FALSE, FALSE, sizeof(gint)); for (i = 0; i < 500000; i++) { g_array_append_val (garray, i); } for (i = 0; i < garray->len; i++) { x = g_array_index (garray, gint, i); } g_array_free (garray, TRUE); } int fib_test (int n) { if (n < 2) return 1; return fib_test (n - 1) + fib_test (n - 2); } #define timefunk(f, n) \ { \ bt = timef(); \ f(n); \ et = timef(); \ printf ("%20s (%d) elapsed: %5.2f seconds\n", #f, n, et - bt); \ } int main (int argc, char *argv[]) { double bt, et; timefunk(fib_test, 30) timefunk(loop_test, 500000); timefunk(index_test, 1000000); timefunk(index_string_test, 1000000); timefunk(nested_int_test, 23); timefunk(returntest, 5000000); timefunk(functest, 5000000); timefunk(objecttest, 5000000) timefunk(file_line_test, 1) timefunk(file_line_split_test, 1) timefunk(list_append_test, 50000) timefunk(list_traverse_test, 500000) return 0; }