summaryrefslogtreecommitdiffstats
path: root/contrib/sendmail/libsm/b-strcmp.c
blob: af5335eb915aec86daefab17244f02eaf0de9023 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
 *      All rights reserved.
 *
 * By using this file, you agree to the terms and conditions set
 * forth in the LICENSE file which can be found at the top level of
 * the sendmail distribution.
 */

#include <sm/gen.h>
SM_RCSID("@(#)$Id: b-strcmp.c,v 1.1.1.1 2002/02/17 21:56:43 gshapiro Exp $")
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sm/string.h>

#define toseconds(x, y)	(x.tv_sec - y.tv_sec)
#define SIZE	512
#define LOOPS	4000000L	/* initial number of loops */
#define MAXTIME	30L	/* "maximum" time to run single test */

void
fatal(str)
	char *str;
{
	perror(str);
	exit(1);
}

void
purpose()
{
	printf("This program benchmarks the performance differences between\n");
	printf("strcasecmp() and sm_strcasecmp().\n");
	printf("These tests may take several minutes to complete.\n");
}

int
main(argc, argv)
	int argc;
	char *argv[];
{
	long a;
	int k;
	bool doit = false;
	long loops;
	long j;
	long one, two;
	struct timeval t1, t2;
	char src1[SIZE], src2[SIZE];

# define OPTIONS	"d"
	while ((k = getopt(argc, argv, OPTIONS)) != -1)
	{
		switch ((char) k)
		{
		  case 'd':
			doit = true;
			break;

		  default:
			break;
		}
	}

	if (!doit)
	{
		purpose();
		printf("If you want to run it, specify -d as option.\n");
		return 0;
	}

	/* Run-time comments to the user */
	purpose();
	printf("\n");
	for (k = 0; k < 3; k++)
	{
		switch (k)
		{
		  case 0:
			(void) sm_strlcpy(src1, "1234567890", SIZE);
			(void) sm_strlcpy(src2, "1234567890", SIZE);
			break;
		  case 1:
			(void) sm_strlcpy(src1, "1234567890", SIZE);
			(void) sm_strlcpy(src2, "1234567891", SIZE);
			break;
		  case 2:
			(void) sm_strlcpy(src1, "1234567892", SIZE);
			(void) sm_strlcpy(src2, "1234567891", SIZE);
			break;
		}
		printf("Test %d: strcasecmp(%s, %s) versus sm_strcasecmp()\n",
			k, src1, src2);
		loops = LOOPS;
		for (;;)
		{
			j = 0;
			if (gettimeofday(&t1, NULL) < 0)
				fatal("gettimeofday");
			for (a = 0; a < loops; a++)
				j += strcasecmp(src1, src2);
			if (gettimeofday(&t2, NULL) < 0)
				fatal("gettimeofday");
			one = toseconds(t2, t1);
			printf("\tstrcasecmp() result: %ld seconds [%ld]\n",
				one, j);

			j = 0;
			if (gettimeofday(&t1, NULL) < 0)
				fatal("gettimeofday");
			for (a = 0; a < loops; a++)
				j += sm_strcasecmp(src1, src2);
			if (gettimeofday(&t2, NULL) < 0)
				fatal("gettimeofday");
			two = toseconds(t2, t1);
			printf("\tsm_strcasecmp() result: %ld seconds [%ld]\n",
				two, j);

			if (abs(one - two) > 2)
				break;
			loops += loops;
			if (loops < 0L || one > MAXTIME)
			{
				printf("\t\t** results too close: no decision\n");
				break;
			}
			else
			{
				printf("\t\t** results too close redoing test %ld times **\n",
					loops);
			}
		}
	}

	printf("\n\n");
	printf("Interpreting the results:\n");
	printf("\tFor differences larger than 2 seconds, the lower value is\n");
	printf("\tbetter and that function should be used for performance\n");
	printf("\treasons.\n\n");
	printf("This program will re-run the tests when the difference is\n");
	printf("less than 2 seconds.\n");
	printf("The result will vary depending on the compiler optimization\n");	printf("level used. Compiling the sendmail libsm library with a\n");
	printf("better optimization level can change the results.\n");
	return 0;
}
OpenPOWER on IntegriCloud