2 * Copyright (c) 2007 Sean C. Farley <scf@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/types.h>
28 #include <sys/resource.h>
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 const char value1
[] = "Large ------------------ value";
41 const char value2
[] = "Small -- value";
42 char nameValuePair
[] = "less=more";
43 const char name
[] = "PATH";
44 const char name2
[] = "SHELL";
45 const int MaxIterations
= 1000000;
46 const char Tabs
[] = "\t\t\t";
50 report_time(const char *action
, struct timeval
*startTime
,
51 struct timeval
*endTime
)
56 actionLen
= strlen(action
);
57 numTabs
= 3 - actionLen
/ 8;
59 return (printf("Time spent executing %s:%.*s%f\n", action
, numTabs
, Tabs
,
60 (endTime
->tv_sec
- startTime
->tv_sec
) +
61 (double)(endTime
->tv_usec
- startTime
->tv_usec
) / 1000000));
66 main(int argc
, char **argv
)
69 struct rusage endUsage
;
70 struct rusage startUsage
;
73 * getenv() on the existing environment.
75 getrusage(RUSAGE_SELF
, &startUsage
);
77 /* Iterate over setting variable. */
78 for (iterations
= 0; iterations
< MaxIterations
; iterations
++)
79 if (getenv(name
) == NULL
)
80 err(EXIT_FAILURE
, "getenv(name)");
82 getrusage(RUSAGE_SELF
, &endUsage
);
84 report_time("getenv(name)", &startUsage
.ru_utime
, &endUsage
.ru_utime
);
88 * setenv() a variable with a large value.
90 getrusage(RUSAGE_SELF
, &startUsage
);
92 /* Iterate over setting variable. */
93 for (iterations
= 0; iterations
< MaxIterations
; iterations
++)
94 if (setenv(name
, value1
, 1) == -1)
95 err(EXIT_FAILURE
, "setenv(name, value1, 1)");
97 getrusage(RUSAGE_SELF
, &endUsage
);
99 report_time("setenv(name, value1, 1)", &startUsage
.ru_utime
,
104 * getenv() the new variable on the new environment.
106 getrusage(RUSAGE_SELF
, &startUsage
);
108 /* Iterate over setting variable. */
109 for (iterations
= 0; iterations
< MaxIterations
; iterations
++)
110 /* Set large value to variable. */
111 if (getenv(name
) == NULL
)
112 err(EXIT_FAILURE
, "getenv(name)");
114 getrusage(RUSAGE_SELF
, &endUsage
);
116 report_time("getenv(name)", &startUsage
.ru_utime
, &endUsage
.ru_utime
);
120 * getenv() a different variable on the new environment.
122 getrusage(RUSAGE_SELF
, &startUsage
);
124 /* Iterate over setting variable. */
125 for (iterations
= 0; iterations
< MaxIterations
; iterations
++)
126 /* Set large value to variable. */
127 if (getenv(name2
) == NULL
)
128 err(EXIT_FAILURE
, "getenv(name2)");
130 getrusage(RUSAGE_SELF
, &endUsage
);
132 report_time("getenv(name2)", &startUsage
.ru_utime
, &endUsage
.ru_utime
);
136 * setenv() a variable with a small value.
138 getrusage(RUSAGE_SELF
, &startUsage
);
140 /* Iterate over setting variable. */
141 for (iterations
= 0; iterations
< MaxIterations
; iterations
++)
142 if (setenv(name
, value2
, 1) == -1)
143 err(EXIT_FAILURE
, "setenv(name, value2, 1)");
145 getrusage(RUSAGE_SELF
, &endUsage
);
147 report_time("setenv(name, value2, 1)", &startUsage
.ru_utime
,
152 * getenv() a different variable on the new environment.
154 getrusage(RUSAGE_SELF
, &startUsage
);
156 /* Iterate over setting variable. */
157 for (iterations
= 0; iterations
< MaxIterations
; iterations
++)
158 /* Set large value to variable. */
159 if (getenv(name2
) == NULL
)
160 err(EXIT_FAILURE
, "getenv(name)");
162 getrusage(RUSAGE_SELF
, &endUsage
);
164 report_time("getenv(name)", &startUsage
.ru_utime
, &endUsage
.ru_utime
);
168 * getenv() a different variable on the new environment.
170 getrusage(RUSAGE_SELF
, &startUsage
);
172 /* Iterate over setting variable. */
173 for (iterations
= 0; iterations
< MaxIterations
; iterations
++)
174 /* Set large value to variable. */
175 if (getenv(name2
) == NULL
)
176 err(EXIT_FAILURE
, "getenv(name2)");
178 getrusage(RUSAGE_SELF
, &endUsage
);
180 report_time("getenv(name2)", &startUsage
.ru_utime
, &endUsage
.ru_utime
);
184 * putenv() a variable with a small value.
186 getrusage(RUSAGE_SELF
, &startUsage
);
188 /* Iterate over setting variable. */
189 for (iterations
= 0; iterations
< MaxIterations
; iterations
++)
190 if (putenv(nameValuePair
) == -1)
191 err(EXIT_FAILURE
, "putenv(nameValuePair)");
193 getrusage(RUSAGE_SELF
, &endUsage
);
195 report_time("putenv(nameValuePair)", &startUsage
.ru_utime
,