home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / perl / 5671 < prev    next >
Encoding:
Text File  |  1992-09-01  |  3.7 KB  |  116 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!europa.asd.contel.com!darwin.sura.net!sgiblab!a2i!dhesi
  3. From: dhesi@rahul.net (Rahul Dhesi)
  4. Subject: Re: randomize.c
  5. Message-ID: <Bty0qF.B5D@rahul.net>
  6. Sender: news@rahul.net (Usenet News)
  7. Nntp-Posting-Host: bolero
  8. Organization: a2i network
  9. References: <Bts28y.DJA@rahul.net> <AAS.92Aug31133620@rosmer.nr.no> <1992Aug31.183903.4143@cirrus.com> <1992Sep1.210332.26149@netlabs.com>
  10. Date: Wed, 2 Sep 1992 08:54:15 GMT
  11. Lines: 103
  12.  
  13. In <1992Sep1.210332.26149@netlabs.com> lwall@netlabs.com (Larry Wall) writes:
  14.  
  15. >This one's weird enough that you might be able
  16. >to beat splice by copying the actual strings like you did.  As it happens,
  17. >on a sparc the splice wins by about 20%.
  18.  
  19. Taking this advice, I went ahead and tried it!  I ran my benchmarks on
  20. a SPARCstation-1+ with 64 megs of main memory.  In addition to
  21. "ize.copy" (my original perl version, which copies lines) and "splice"
  22. (splice version, from camel book p20), I also tried "ize.ind", which
  23. only copies integers, at the cost of an extra level of indirection for
  24. subscripting.
  25.  
  26. This turned out to be quite a bit of fun.
  27.  
  28.      program     no. of data lines     real time, min:sec
  29.      -------     ----------            ------------------
  30.      ize.copy      1,914               0:01.42
  31.      ize.ind       1,914               0:01.35
  32.      splice        1,914               0:01.06
  33.      ize.copy     78,474               0:53
  34.      ize.ind      78,474               1:01
  35.      splice       78,474               6:51
  36.  
  37. For typical uses (small amount of input), there isn't much difference.
  38. For atypical uses (e.g., randomizing 40 csh manuals), the traditional
  39. array method wins over splice.
  40.  
  41. Session transcript follows, and then sources for each program.
  42.  
  43. ===== begin edited session log =====
  44. % ls -F
  45. ize.copy*  ize.ind*   splice*
  46. % man csh | col -b > small.data
  47. % repeat 40 cat small.data >> big.data
  48. % wc -l *.data
  49.    76560 big.data
  50.     1914 small.data
  51.    78474 total
  52. % time ize.copy < small.data > junk
  53. 1.1u 0.2s 0:01.41 97.1% 0+430k 3+12io 0pf+0w
  54. % time ize.ind < small.data > junk
  55. 1.1u 0.2s 0:01.36 101.4% 0+468k 0+12io 0pf+0w
  56. % time splice < small.data > junk
  57. 0.8u 0.2s 0:01.06 102.8% 0+422k 0+8io 0pf+0w
  58. % time ize.copy < small.data > junk
  59. 1.1u 0.2s 0:01.35 101.4% 0+429k 0+9io 0pf+0w
  60. % time ize.ind < small.data > junk
  61. 1.1u 0.2s 0:01.34 101.4% 0+469k 0+9io 0pf+0w
  62. % time splice < small.data > junk
  63. 0.8u 0.2s 0:01.06 102.8% 0+420k 0+9io 0pf+0w
  64. % time ize.copy < big.data > junk
  65. 43.3u 8.5s 0:52.76 98.2% 0+8217k 1+363io 0pf+0w
  66. % time ize.ind < big.data > junk
  67. 42.8u 17.5s 1:01.76 97.8% 0+10278k 2+361io 0pf+0w
  68. % time splice < big.data > junk
  69. 379.8u 14.9s 6:52.05 95.7% 0+8861k 3+384io 0pf+0w
  70. % time ize.copy < big.data > junk
  71. 43.1u 8.8s 0:52.94 98.2% 0+8179k 7+362io 0pf+0w
  72. % time ize.ind < big.data > junk
  73. 42.9u 15.5s 0:59.25 98.7% 0+10226k 4+362io 0pf+0w
  74. % time splice < big.data > junk
  75. 379.4u 12.5s 6:51.03 95.3% 0+8853k 10+385io 0pf+0w
  76.  
  77. ===== begin ize.copy =====
  78. #! /usr/bin/perl
  79. # Copy input lines output in random order.  R. Dhesi 1992/08/29.
  80. @lines = <STDIN>;
  81. $limit = $#lines;
  82. $first = 0;
  83. srand;
  84.  
  85. while ($first <= $limit) {
  86.    $r = rand($limit - $first) + $first;
  87.    print $lines[$r];
  88.    $lines[$r] = $lines[$first++];
  89. }
  90. ===== begin ize.ind =====
  91. #! /usr/bin/perl
  92. # Copy input lines output in random order.  R. Dhesi 1992/09/02.
  93. @lines = <STDIN>;
  94. $limit = $#lines;
  95. @ind  = (0 .. $limit);    # indirect indexes
  96. $first = 0;
  97. srand;
  98.  
  99. while ($first <= $limit) {
  100.    $r = rand($limit - $first) + $first;
  101.    print $lines[$ind[$r]];
  102.    $ind[$r] = $first++;
  103. }
  104. ===== begin splice =====
  105. #! /usr/bin/perl
  106. # from camel book p20
  107. srand;
  108. @lines=<>;
  109. while (@lines) {
  110.    print splice(@lines, rand @lines, 1);
  111. }
  112. ===== end =====
  113. --
  114. Rahul Dhesi <dhesi@rahul.net>
  115. also:  dhesi@cirrus.com
  116.