75% speedup for zdump on 64-bit Solaris sparc

Paul Eggert eggert at CS.UCLA.EDU
Sun Feb 26 09:35:52 UTC 2006


Ken Pizzini <tz. at explicate.org> writes:

> On Fri, Feb 24, 2006 at 01:28:43PM -0800, Paul Eggert wrote:
>> +		int lo = 1;
>> +		int hi = sp->timecnt;
>> +		for (;;) {
>> +			i = (lo + hi) >> 1;
>> +			if (hi <= lo)
>>  				break;
>> +			if (t < sp->ats[i])
>> +				hi = i;
>> +			else
>> +				lo = i + 1;
>> +		}
>>  		i = (int) sp->types[i - 1];
>
> The loop control on that strikes me as a bit odd (not wrong, just odd).
> How about using simple "while" instead?
> 		int lo = 1;
> 		int hi = sp->timecnt;
> 		while (lo < hi) {
> 			i = (lo + hi) >> 1;
> 			if (t < sp->ats[i])
> 				hi = i;
> 			else
> 				lo = i + 1;
> 		}
> 		i = (int) sp->types[lo - 1];
>
> (My guess is that you were unintentionally intent on keeping the
> "i" in the sp->types[] dereference of the original code, for which
> your code is about as good as it gets.)

Yes, your guess is right.  I like your simple "while" better.  Thanks.

Better yet would be to not reuse "i", e.g., something like this:

		int lo = 1;
		int hi = sp->timecnt;
		while (lo < hi) {
			int mid = (lo + hi) >> 1;
			if (t < sp->ats[mid])
				hi = mid;
			else
				lo = mid + 1;
		}
		i = (int) sp->types[lo - 1];



More information about the tz mailing list