#!../src/tops  -s ../sys  -u ../usr  N 200
#!/usr/local/bin/tops  N 200
{
 Running the hash problem posed in:
    The Great Computer Language Shootout (2001):
       http://www.bagley.org/~doug/shootout/
    Shootout tests were run on a dual 450Mhz Pentium-II server with
    1GB of RAM and 4x9GB SCSI disks.

    The problem:
       From 1 to N, count the numbers whose hex notation is the same
       as some number's decimal notation.

    More in notes at the bottom of this file.

------------------------------------------------------------------------

To run this script for N built into #! line above:
   [dale@clacker] /opt/tops/tops/test > shoot

To run this script for M given on the command line:
   [dale@clacker] /opt/tops/tops/test > shoot M 80000
    Starting...
    For M = 80000
    13880 matches
    8 sec
    1949517 added bytes for %HASH
    7 added bytes after close %HASH
   [dale@clacker] /opt/tops/tops/test > 

}
memcat push

" Starting..." . nl

"M" argv (qS) any?  
IF " For M = " . (qS)
ELSE "N" argv (qS) any? not
   IF "80000" THEN 
   " For N = " . (qS)
THEN (qS) 

number drop into N 
N .u nl

time push \ timing this region (one second resolution):

\  Hash make:
   1 N items int8 "%llX" format (key)
   1 N items integer (value) N "%HASH" hash_make

\  Hash lookup:
   %HASH 1 N items reversed integer "%0.0f" format (key1)
   (hHash hKey) hash_lookup pilen rows .i " matches" . nl

time pull - .i " sec" . nl

memcat peek - MILLION * .i " added bytes for %HASH" . nl

%HASH hash_close

memcat pull - MILLION * .i " added bytes after close %HASH" . nl

halt

------------------------------------------------------------------------

Here is the hash problem used in the The Great Computer Language 
Shootout referenced above:

http://cm.bell-labs.com/cm/cs/who/bwk/interps/pap.html

Timing Trials, or, the Trials of Timing: 
Experiments with Scripting and User-Interface Languages 

     Brian W. Kernighan
     Bell Laboratories
     Murray Hill, NJ 07974
     bwk@bell-labs.com 

     Christopher J. Van Wyk
     Department of Mathematics and Computer Science
     Drew University
     Madison, NJ 07940
     cvanwyk@drew.edu 

...

Associative Data Structures 

Several of these languages offer a built-in facility (variously called 
associative arrays, hash tables or hashes) that can be used to associate
values with keys so that lookups can be performed in constant time. Our
test program synthesizes key values from numbers, to avoid performing 
any I/O. In order to exercise both lookups that succeed and lookups that
fail, the program stores keys created from the numbers 1 to n expressed
as hexadecimal strings, but attempts to retrieve keys using the numbers
1 to n expressed as decimal strings. The program in Perl is 

#!/usr/local/bin/perl -w
#!/usr/bin/env perl

$c=0;
$n=80000;
for ($i = 1; $i <= $n; $i++) {
    $X{sprintf('%x', $i)} = $i;
}
for ($i = $n; $i > 0; $i--) {
    if (defined $X{$i}) {
        $c++;
    }
}
print $c;
