Sorry to revive an old thread. One thing to note is that dividing a 64-bit by a 32-bit is much faster than dividing a 64-bit by a 64-bit, at least on recent Intel platforms. Unfortunately there is no valid way to express that kind of calculation in Go..
Is the compiler/optimizer smart enough to figure out it can do that if you do something like `uint32(a / uint64(b))` where a is `uint64` and b is `uint32`? (the result of that expression can be computed using the 32-bit version of DIV) On Saturday, January 19, 2013 at 6:10:34 PM UTC-5, Michael Jones wrote: > > Integer division is slow. > > It is the unloved arithmetic operation in CPU design because of its low > frequency of occurrence in the instruction stream. It is the one--if you > remember childhood schooling--that was "not like the others" in its > mechanism: there was lots of guessing using leading digits, trial divisors, > and so on. This awkwardness exists even in base 2 (though much less) and > means that any division circuit my have to do at least one "fixup" step. (A > good design does either zero or one fixup, but that one means more work and > plausibly an extra cycle on what is already a long cycle-count activity.) > > As one specific example, when adding and subtracting were 1 cycle and > multiply was 4, a 32-bit integer division required 19 cycles. > > > On Sat, Jan 19, 2013 at 1:25 PM, minux <minu...@gmail.com <javascript:>> > wrote: > >> >> >> On Sun, Jan 20, 2013 at 5:22 AM, Jan Mercl <0xj...@gmail.com >> <javascript:>> wrote: >> >>> On Sat, Jan 19, 2013 at 10:13 PM, Dominik Honnef <domi...@fork-bomb.org >>> <javascript:>> wrote: >>> >>> Cannot reproduce here (Xeon on dont-know-how-much GHz) >>> >>> jnml@fsc-r630:~/src/tmp/20130119$ cat a_test.go >>> package main >>> >>> import ( >>> "testing" >>> ) >>> >>> func BenchmarkBase(b *testing.B) { >>> var x int32 >>> for i := 1; i < b.N; i++ { >>> } >>> _ = x >>> } >>> >>> func BenchmarkInt(b *testing.B) { >>> var x int >>> for i := 1; i < b.N; i++ { >>> x /= 42 >>> } >>> _ = x >>> } >>> >> division by a constant should be turned into multiplication by magic >> constants >> by the Go compiler just to avoid the costly divide instruction. >> >> you can verify that by 'go tool 6g -S'. >> >> -- >> >> >> > > > > -- > Michael T. Jones | Chief Technology Advocate | m...@google.com > <javascript:> | +1 650-335-5765 > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.