File: format_compare_speed.c

package info (click to toggle)
libqb 2.0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,092 kB
  • sloc: ansic: 22,191; sh: 5,232; makefile: 607
file content (97 lines) | stat: -rw-r--r-- 2,635 bytes parent folder | download | duplicates (5)
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
/*
 * Copyright (c) 2013 Red Hat, Inc.
 *
 * All rights reserved.
 *
 * Author: Angus Salkeld <asalkeld@redhat.com>
 *
 * This file is part of libqb.
 *
 * libqb is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * libqb is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with libqb.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "os_base.h"

#include <qb/qbdefs.h>
#include <qb/qbutil.h>
#include <qb/qblog.h>

extern size_t qb_vsnprintf_serialize(char *serialize, size_t max_len, const char *fmt, va_list ap);

static void
store_this_qb(const char *fmt, ...) __attribute__((format(printf, 1, 2)));

static void
store_this_snprintf(const char *fmt, ...) __attribute__((format(printf, 1, 2)));

typedef void (*snprintf_like_func)(const char *fmt, ...)  __attribute__((format(printf, 1, 2)));


static void
store_this_qb(const char *fmt, ...)
{
	char buf[QB_LOG_MAX_LEN];
	va_list ap;

	va_start(ap, fmt);
	qb_vsnprintf_serialize(buf, QB_LOG_MAX_LEN, fmt, ap);
	va_end(ap);
}

static void
store_this_snprintf(const char *fmt, ...)
{
	char buf[QB_LOG_MAX_LEN];
	va_list ap;

	va_start(ap, fmt);
	vsnprintf(buf, QB_LOG_MAX_LEN, fmt, ap);
	va_end(ap);
}

#define ITERATIONS 10000000

static void
test_this_one(const char *name, snprintf_like_func func)
{
	unsigned i;
	qb_util_stopwatch_t *sw = qb_util_stopwatch_create();
	float elapsed = 452.245252343;
	float ops_per_sec = 0.345624523;

	qb_util_stopwatch_start(sw);
	for (i = 0; i < ITERATIONS; i++) {
		func("%u %s %llu %9.3f", i, "hello", 3425ULL, elapsed);
		func("[%10s] %.32xd -> %p", "hello", i, func);
		func("Client %s.%.9s wants to fence (%s) '%s' with device '%3.5f'",
		     "bla", "foooooooooooooooooo",
		     name, "target", ops_per_sec);
		func("Node %s now has process list: %.32x (was %.32x)",
		     "18builder", 2U, 0U);
	}
	qb_util_stopwatch_stop(sw);
	elapsed = qb_util_stopwatch_sec_elapsed_get(sw);
	ops_per_sec = ((float)ITERATIONS) / elapsed;
	printf("%s] Duration: %9.3f OPs/sec: %9.3f\n", name, elapsed, ops_per_sec);
	qb_util_stopwatch_free(sw);
}

int
main(void)
{
	test_this_one("qb store", store_this_qb);
	test_this_one("snprintf", store_this_snprintf);

	return 0;
}