glib/glib/tests/bitlock.c
Ryan Lortie d09443fe20 optimise bitlocks with new atomic operations
Add a performance test for bitlocks.

Make use of the new g_atomic_int_{and,or} to avoid having to do the
usual compare-and-exchange loop.

On a test machine, the change increases performance from approximately
20 million uncontended acquire/releases per second to 31 million.
2011-05-28 17:39:12 -04:00

40 lines
674 B
C

#include <glib.h>
#define ITERATIONS 100000000
static void
test_bitlocks (void)
{
guint64 start = g_get_monotonic_time ();
gint lock = 0;
gint i;
for (i = 0; i < ITERATIONS; i++)
{
g_bit_lock (&lock, 0);
g_bit_unlock (&lock, 0);
}
{
gdouble elapsed;
gdouble rate;
elapsed = g_get_monotonic_time () - start;
elapsed /= 1000000;
rate = ITERATIONS / elapsed;
g_test_maximized_result (rate, "iterations per second");
}
}
int
main (int argc, char **argv)
{
g_test_init (&argc, &argv, NULL);
if (g_test_perf ())
g_test_add_func ("/bitlock/performance/uncontended", test_bitlocks);
return g_test_run ();
}