Skip to content

Instantly share code, notes, and snippets.

@kristate
Created September 16, 2012 18:41
Show Gist options
  • Save kristate/3733696 to your computer and use it in GitHub Desktop.
Save kristate/3733696 to your computer and use it in GitHub Desktop.
test on how to copy worker env for spawning
kristate@www12294uf:~$ gcc test1.c; ./a.out
type_a: 1.900000 seconds
type_b: 1.420000 seconds
kristate@www12294uf:~$ gcc test1.c; ./a.out
type_a: 1.790000 seconds
type_b: 1.430000 seconds
kristate@www12294uf:~$ gcc test1.c; ./a.out
type_a: 1.480000 seconds
type_b: 1.480000 seconds
kristate@www12294uf:~$ gcc test1.c; ./a.out
type_a: 1.510000 seconds
type_b: 1.450000 seconds
kristate@www12294uf:~$ gcc test1.c; ./a.out
type_a: 1.670000 seconds
type_b: 1.680000 seconds
kristate@www12294uf:~$ gcc test1.c; ./a.out
type_a: 1.640000 seconds
type_b: 1.660000 seconds
kristate@www12294uf:~$ gcc test1.c; ./a.out
type_a: 2.200000 seconds
type_b: 1.630000 seconds
kristate@www12294uf:~$ gcc test1.c; ./a.out
type_a: 1.860000 seconds
type_b: 1.430000 seconds
kristate@www12294uf:~$ gcc test1.c; ./a.out
type_a: 2.020000 seconds
type_b: 1.430000 seconds
kristate@www12294uf:~$ gcc test1.c; ./a.out
type_a: 1.570000 seconds
type_b: 1.420000 seconds
kristate@www12294uf:~$
/*
gcc env_copy_test.c; ./a.out
*/
#include <stdio.h>
#include <time.h> /* clock */
#include <stdlib.h> /* free */
#include <string.h> /* sprintf */
extern char **environ;
static inline void type_a(const char *worker_id, const char *ipc_filename) {
int env_cnt = 0;
char **entry;
for (entry = environ; *entry; entry++, env_cnt++);
char *worker_env[2 + env_cnt + 1];
worker_env[0] = malloc(sizeof("LEN_WORKER_ID=") + strlen(worker_id));
sprintf(worker_env[0], "LEV_WORKER_ID=%s", worker_id);
worker_env[1] = malloc(sizeof("LEV_IPC_FILENAME=") + strlen(ipc_filename));
sprintf(worker_env[1], "LEV_IPC_FILENAME=%s", ipc_filename);
memcpy(worker_env + 2, environ, sizeof(char *) * (env_cnt + 1));
free( worker_env[0] ); /* free LEV_WORKER_ID */
free( worker_env[1] ); /* free LEV_IPC_FILENAME */
}
static inline void type_b(const char *worker_id, const char *ipc_filename) {
int n = 0;
char **env_item;
char env_temp[1024];
char *worker_env[256];
sprintf(env_temp, "LEV_WORKER_ID=%s", worker_id);
worker_env[n++] = strdup(env_temp);
sprintf(env_temp, "LEV_IPC_FILENAME=%s", ipc_filename);
worker_env[n++] = strdup(env_temp);
for (env_item=environ;*env_item&&n<254;env_item++) {
worker_env[n++] = *env_item;
}
worker_env[n] = NULL; /* terminate */
free( worker_env[0] ); /* free LEV_WORKER_ID */
free( worker_env[1] ); /* free LEV_IPC_FILENAME */
}
int main(void)
{
int i;
clock_t begin, end;
char worker_id[8];
char *ipc_filename = "/tmp/lev_12345.tmp";
begin = clock();
for (i=0;i<2000000;i++) {
sprintf(worker_id, "%d", i);
type_a(worker_id, ipc_filename);
}
end = clock();
printf("type_a: %f seconds\n", (double)(end - begin) / CLOCKS_PER_SEC);
begin = clock();
for (i=0;i<2000000;i++) {
sprintf(worker_id, "%d", i);
type_b(worker_id, ipc_filename);
}
end = clock();
printf("type_b: %f seconds\n", (double)(end - begin) / CLOCKS_PER_SEC);
return 0;
}
@kristate
Copy link
Author

GIST for: connectFree/lev#66

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment