2 ** Copyright 2010 The Android Open Source Project
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
8 ** http://www.apache.org/licenses/LICENSE-2.0
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
18 * Some quick and dirty micro-benchmarks
31 static void tv_sub(struct timeval *tv2, struct timeval *tv1) {
32 tv2->tv_sec -= tv1->tv_sec;
33 tv2->tv_usec -= tv1->tv_usec;
34 while (tv2->tv_usec < 0) {
35 tv2->tv_usec += 1000000;
40 static int do_sleep(int iters, int delay) {
45 for (i = 0; iters == -1 || i < iters; i++) {
46 gettimeofday(&tv1, NULL);
48 gettimeofday(&tv2, NULL);
52 printf("sleep(%d) took %ld.%06ld seconds\n", delay, tv2.tv_sec, tv2.tv_usec);
60 static int do_cpu(int iters, int a) {
65 for (i = 0; iters == -1 || i < iters; i++) {
66 gettimeofday(&tv1, NULL);
67 for (cpu_foo = 0; cpu_foo < 100000000; cpu_foo++);
68 gettimeofday(&tv2, NULL);
72 printf("cpu took %ld.%06ld seconds\n", tv2.tv_sec, tv2.tv_usec);
77 static double mb_sec(unsigned long bytes, struct timeval *delta) {
78 unsigned long us = delta->tv_sec * 1000000 + delta->tv_usec;
79 return (double)bytes * 1000000.0 / 1048576.0 / (double)us;
82 static int do_memset(int iters, int sz) {
87 uint8_t *b = malloc(sz);
89 int c = 1000000000/sz;
91 for (i = 0; iters == -1 || i < iters; i++) {
92 gettimeofday(&tv1, NULL);
93 for (j = 0; j < c; j++)
96 gettimeofday(&tv2, NULL);
100 printf("memset %dx%d bytes took %ld.%06ld seconds (%f MB/s)\n",
101 c, sz, tv2.tv_sec, tv2.tv_usec, mb_sec(c*sz, &tv2));
106 static int do_memcpy(int iters, int sz) {
111 uint8_t *a = malloc(sz);
113 uint8_t *b = malloc(sz);
115 int c = 1000000000/sz;
117 for (i = 0; iters == -1 || i < iters; i++) {
118 gettimeofday(&tv1, NULL);
119 for (j = 0; j < c; j++)
122 gettimeofday(&tv2, NULL);
126 printf("memcpy %dx%d bytes took %ld.%06ld seconds (%f MB/s)\n",
127 c, sz, tv2.tv_sec, tv2.tv_usec, mb_sec(c*sz, &tv2));
134 static int do_memread(int iters, int sz) {
141 int c = 1000000000/sz;
143 for (i = 0; iters == -1 || i < iters; i++) {
144 gettimeofday(&tv1, NULL);
145 for (j = 0; j < c; j++)
146 for (k = 0; k < sz/4; k++)
149 gettimeofday(&tv2, NULL);
153 printf("read %dx%d bytes took %ld.%06ld seconds (%f MB/s)\n",
154 c, sz, tv2.tv_sec, tv2.tv_usec, mb_sec(c*sz, &tv2));
161 int (*ptr)(int, int);
162 } function_table[] = {
165 {"memset", do_memset},
166 {"memcpy", do_memcpy},
167 {"memread", do_memread},
171 static void usage() {
175 for (i = 0; function_table[i].name; i++) {
176 printf("\tmicro_bench %s ARG [ITERS]\n", function_table[i].name);
180 int main(int argc, char **argv) {
184 if (argc < 3 || argc > 4) {
191 iters = atoi(argv[3]);
193 for (i = 0; function_table[i].name; i++) {
194 if (!strcmp(argv[1], function_table[i].name)) {
195 printf("%s\n", function_table[i].name);
196 return (*function_table[i].ptr)(iters, atoi(argv[2]));