Re: [CHALLENGE] finding rightmost zero bit
- From: Michel OLAGNON <molagnon@xxxxxxxxxxxxxxxxx>
- Date: Tue, 30 Aug 2005 09:38:50 +0200
Bart Vandewoestyne wrote:
On 2005-08-29, Michel OLAGNON <molagnon@xxxxxxxxxxxxxxxxx> wrote:
I claim the slowest
bartv@vonneumann:~/fortran$ ./time_findpos huge(n) is 2147483647.
Enter first number: 1
Enter last number: 147483647
Total bits counted: 294967286
Bart1's method did it from 1 to 147483647 in 1.82 seconds.
Total bits counted: 294967286
Bart2's method did it from 1 to 147483647 in 1.88 seconds.
Total bits counted: 294967286
Bart3's method did it from 1 to 147483647 in 1.89 seconds.
Total bits counted: 294967286
Michel's method did it from 1 to 147483647 in 18.50 seconds.
*fwew*... that's indeed slow :-)
OK. Here is a reasonable version of the same algorithm:
function findpos_mich3(n) result (pos)
integer(kind=i4b), intent(in) :: n
integer(kind=i4b) :: pos
integer(kind=i4b) :: temptemp = ieor(n,n+1)+1
select case (temp)
case (0)
pos = bit_size(n)
case (1)
pos = bit_size(n) + 1
case (2)
pos = 1
case (4)
pos = 2
case (8)
pos = 3
case (16)
pos = 4
case (32)
pos = 5
case (64)
pos = 6
case (128)
pos = 7
case (256)
pos = 8
case (512)
pos = 9
case (1024)
pos = 10
case (2048)
pos = 11
case (4096)
pos = 12
case (8192)
pos = 13
case (16384)
pos = 14
case (32768)
pos = 15
case default
pos = 15
do
temp = temp / 65536
select case (temp)
case (1)
pos = pos + 1
case (2)
pos = pos + 2
case (4)
pos = pos + 3
case (8)
pos = pos + 4
case (16)
pos = pos + 5
case (32)
pos = pos + 6
case (64)
pos = pos + 7
case (128)
pos = pos + 8
case (256)
pos = pos + 9
case (512)
pos = pos + 10
case (1024)
pos = pos + 11
case (2048)
pos = pos + 12
case (4096)
pos = pos + 13
case (8192)
pos = pos + 14
case (16384)
pos = pos + 15
case (32768)
pos = pos + 16
case default
pos = pos + 16
cycle
end select
exit
enddo
end selectend function findpos_mich3
but it's a one-liner:
pos = nint (log (real(ieor(n,n+1)+1, kind=renough)) / log (2.0_renough))
:-)
OK. Bonus-point for you :-)
Regards, Bart
.
- Follow-Ups:
- Re: [CHALLENGE] finding rightmost zero bit
- From: Bart Vandewoestyne
- Re: [CHALLENGE] finding rightmost zero bit
- References:
- [CHALLENGE] finding rightmost zero bit
- From: Bart Vandewoestyne
- Re: [CHALLENGE] finding rightmost zero bit
- From: Michel OLAGNON
- Re: [CHALLENGE] finding rightmost zero bit
- From: Bart Vandewoestyne
- [CHALLENGE] finding rightmost zero bit
- Prev by Date: Re: Undefined array elements
- Next by Date: Re: [CHALLENGE] finding rightmost zero bit
- Previous by thread: Re: [CHALLENGE] finding rightmost zero bit
- Next by thread: Re: [CHALLENGE] finding rightmost zero bit
- Index(es):
Relevant Pages
|
|