The Boston Diaries

The ongoing saga of a programmer who doesn't live in Boston, nor does he even like Boston, but yet named his weblog/journal “The Boston Diaries.”

Go figure.

Wednesday, November 28, 2007

Stupid multithreaded benchmarks

Dan Lyke ran a stupid benchmark—just incrementing a variable to a billion, one just flat out, and one between some pthreads locking primitives.

I wanted to see what stupid results I would get if I used spin locks. First, the relevant bit of code (used nasm to compile it under a 2.6GHz dual-core Pentium running Linux 2.6):

		bits	32
		global	t1
		global	t2

		section	.data
gv		dd	0
glock		dd	0

		section	.text

		align	16
t1:		mov	eax,[gv]
		inc	eax
		mov	[gv],eax
		cmp	eax,1000000000
		jl	t1

		; ret

	; the following implements _exit()
	; for the multithreaded version

		xor	ebx,ebx
		mov	eax,252
		int	$80
		mov	eax,1
		int	$80

		align	16
t2:		mov	al,1
t2.wait:	xchg	al,[glock]
		or	al,al
		jne	t2.wait	mov	eax,[gv]
		inc	eax
		mov	[gv],eax
		mov	byte [glock],0
		cmp	eax,1000000000
		jl	t2
		xor	ebx,ebx
		mov	eax,252
		int	$80
		mov	eax,1
		int	$80

Straightforward implementations here. t1() is the straight through counting routine, while t2() is the one with the spin lock. Running single threaded yielded these results:

Counting, single threaded
routine time to execute
t1() 2.454s
t2() 39.752s

While I expected the spin lock to be faster than the pthread locking, I wasn't expecting it to be this slow. But maybe, just maybe, I'll get some of that speed back by running dual threads. At the very least, it should be a bit faster than single core, right?


Bueller? Bueller?

Counting, dual-threaded
routine time to execute
t1() 0m10.334s
t2() 2m31.307s

Um …


I didn't expect spinlocks to be so expensive.


Obligatory Picture

[“I am NOT a number, I am … a Q-CODE!”]

Obligatory Contact Info

Obligatory Feeds

Obligatory Links

Obligatory Miscellaneous

You have my permission to link freely to any entry here. Go ahead, I won't bite. I promise.

The dates are the permanent links to that day's entries (or entry, if there is only one entry). The titles are the permanent links to that entry only. The format for the links are simple: Start with the base link for this site:, then add the date you are interested in, say 2000/08/01, so that would make the final URL:

You can also specify the entire month by leaving off the day portion. You can even select an arbitrary portion of time.

You may also note subtle shading of the links and that's intentional: the “closer” the link is (relative to the page) the “brighter” it appears. It's an experiment in using color shading to denote the distance a link is from here. If you don't notice it, don't worry; it's not all that important.

It is assumed that every brand name, slogan, corporate name, symbol, design element, et cetera mentioned in these pages is a protected and/or trademarked entity, the sole property of its owner(s), and acknowledgement of this status is implied.

Copyright © 1999-2024 by Sean Conner. All Rights Reserved.