home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / awk / awk_rev3.zip / AWK2.REV < prev    next >
Text File  |  1990-05-22  |  14KB  |  309 lines

  1.  
  2.  
  3.         Four AWK Implementations for MS-DOS - How Do They Compare?
  4.               Copyright (c) 1989, 1990, by George A. Theall
  5.  
  6.  
  7.  
  8.  
  9.    In the fall of 1988 I was introduced to a little known programming
  10. language named AWK. Its main feature is undoubtedly the speed with which
  11. programs can be developed. AWK has been available on Unix systems for
  12. about 10 years now but only recently crossed over to the MS-DOS world. 
  13. Despite this slow start, AWK's power, versatility, and flexibility should
  14. make it a hit for anyone who is serious about using their PC. 
  15.  
  16.  
  17.    When I first discovered AWK, it seemed perfectly suited to the type of
  18. data manipulation which much of my research work involves. At the time,
  19. two companies were marketing implementations of AWK for MS-DOS: Mortice
  20. Kern Systems (MKS) and Polytron/SAGE Software. Both claimed to have a
  21. complete implementation of AWK as described in _The AWK Programming
  22. Language_ by Aho, Kernighan, and Weinberger, the language's developers. I
  23. decided on MKS' product - it was the cheaper, and support was available
  24. via electronic mail. 
  25.  
  26.  
  27.    Initially I was a bit uncomfortable with my decision. Though both
  28. companies have excellent reputations, I had not seen any comparisons of
  29. the two AWKs. Since then I have worked with the products from MKS and
  30. Polytron/SAGE as well as non-commercial implementations from Rob Duff and
  31. the GNU Project. Given my experience, two questions come to mind: How
  32. significantly do these implementations differ? And more importantly, why
  33. spend roughly $100 for a commercial program when you can download a copy
  34. of Duff's AWK or GNU AWK from a local bbs for just the cost of a phone
  35. call?
  36.  
  37.  
  38.    All four implementations reviewed here claim to conform to the de facto
  39. standard of _The AWK Programming Language_. And, with the exception of
  40. Duff AWK's inability to handle the pipe form of the getline function, all
  41. four uphold this claim. Some basic features of each are tabulated below:
  42.  
  43.  
  44.                TABLE 1.  Features of Each AWK Implementation
  45.  
  46. ---------------------------------------------------------------------------
  47. Feature                       DUFF_AWK    GAWK        MKS_AWK     POLY_AWK
  48. ---------------------------   ---------   ---------   ---------   ---------
  49. Executable Size (in Kbytes)   63          131         56/87       131
  50. Uses 80x87?                   unknown     unknown     yes (1)     yes
  51. Mamimum record size           1024        16384       2048        32000
  52. Extensions?                   no          yes (2)     no          yes (3)
  53. Read programs from stdin?     yes         no          no          no
  54. ---------------------------------------------------------------------------
  55. (1) MKS AWK comes with four executables - one set of two provides direct 
  56.     support for a numeric coprocessor; the other uses it if available. 
  57.     File sizes reported above are for the set with indirect support.
  58.  
  59. (2) Chief among GAWK's extensions are an IGNORECASE variable as well as 
  60.     egrep-style regular expressions as described by the POSIX standard.
  61.  
  62. (3) PolyAWK's extensions relate to case-conversion, manipulation of 
  63.     environment variables, and bitwise operators.
  64.  
  65. DUFF_AWK represents version 2.14 of Rob Duff's implementation; GAWK refers
  66. to version 2.11, patchlevel 1, of GNU AWK as ported to MS-DOS by Kent
  67. Williams; MKS_AWK denotes version 2.3 of the small and large models from
  68. MKS; and POLY_AWK refers to version 1.3 of Polytron/SAGE's product.
  69.  
  70.  
  71.    To compare the implementations I devised a set of programs based on
  72. tasks for which I commonly use AWK. Each program processes three input
  73. files, constructed of lines such as:
  74.  
  75.      PD1:<MSDOS.APL>
  76.         SAPLPC-A.ARC  208K  BINARY  04/02/88  
  77.         SAPLPC-B.ARC  225K  BINARY  04/02/88  
  78.  
  79.      PD1:<MSDOS.ARC-LBR>
  80.         ADIR103.ARC     8K  BINARY  05/24/87  5-col .ARC file ...
  81.         ADIR140.ARC    10K  BINARY  02/05/88  Dave Rand's ARC ...
  82.  
  83. The input files themselves differ only in their sizes, which are:
  84.  
  85.           112     936    7449 SMALL.FIL
  86.          1006    7199   60711 MEDIUM.FIL
  87.         10569   74685  631238 LARGE.FIL
  88.  
  89. as reported by the word-counting task. [Fields are as follows: number of
  90. lines, number of words, number of characters, and file name.] These tasks
  91. do not purport to represent all of AWK's capabilities nor is there much
  92. justification for selecting them. Nevertheless, they do point to some
  93. interesting differences. 
  94.  
  95.  
  96.    All tasks were run under MS-DOS v3.30 on an NEC PowerMate SX machine
  97. operating at 16MHz and equipped with a fast (28ms) hard disk but without a
  98. math coprocessor. Before each implementation was tested, the hard disk was
  99. optimized and the system was rebooted with no AUTOEXEC.BAT or CONFIG.SYS
  100. file. This resulted in a "clean" system with roughly 600K of conventional
  101. memory available. For each implementation, six tasks were performed using
  102. the three input files, results were tabulated, and then the executable and
  103. output files were moved onto a floppy. To avoid _human_ measurement
  104. inaccuracies execution times were calculated with Brant Cheikes' TM
  105. utility, which rounds to the nearest second.
  106.  
  107.  
  108.    Results are presented in Table 2 below. For each version and each task,
  109. three execution times are reported - the times required to process
  110. SMALL.FIL, MEDIUM.FIL, and LARGE.FIL respectively. The actual AWK programs
  111. appear at the end of this document. 
  112.  
  113.  
  114.                     TABLE 2.  AWK Program Execution Times
  115.                                  (in seconds)
  116.  
  117. ----------------------------------------------------------------------------
  118. Task           DUFF_AWK     GAWK         MKS_AWK      MKS_AWKL     POLY_AWK
  119. -------------  ---------    ---------    ---------    --------     ---------
  120. Record Count.  2/14/145     2/3/21       1/2/12       1/2/23       1/2/15       
  121. Word Count.    4/25/252     3/16/160     1/5/43       2/6/52       2/5/42       
  122. Line Number.   4/21/209     1/7/67       1/6/62       1/6/70       2/6/55       
  123. Reg. Expr.     4/22/222     2/5/40       2/15/150     4/23/228     2/5/41       
  124. Column sums    3/23/238     2/8/78       2/4/36       1/6/54       1/4/39       
  125. Spelling       15/22*/22*   12/123/138*  6/13*/13*    8/71/1020*   4/25/127*    
  126. ----------------------------------------------------------------------------
  127. * indicates the program ran out of memory and aborted.
  128.  
  129. DUFF_AWK represents version 2.14 of Rob Duff's implementation; GAWK refers
  130. to version 2.11, patchlevel 1, of GNU AWK as ported to MS-DOS by Kent
  131. Williams; MKS_AWK and MKS_AWKL denote versions 2.3 of the small and large
  132. models from MKS; and POLY_AWK refers to version 1.3 of Polytron/SAGE's
  133. product.
  134.  
  135.  
  136.    Note that while _actual_ execution times will vary from one situation
  137. or machine to another, _relative_ times are useful in making comparisons. 
  138. Note also that the figures reported above are from a single run rather
  139. than averages of multiple runs. The problem with multiple runs is one of
  140. time: it takes about 1 hour for a single set of runs on the SX!
  141.  
  142.  
  143.    Among the commercial products, there is no clear-cut leader. For tasks
  144. involving SMALL.FIL, execution times for the three implementations are all
  145. within a few seconds of each other so that observed differences are
  146. probably due largely to TM's rounding to the nearest second. As the size
  147. of the input file grows, however, two things become clear: First, the need
  148. to use far pointers exacts a noticeable performance penalty in MKS_AWKL.
  149. Second, POLY_AWK excels at regular expression matching (as exemplified by
  150. the regular-expressions and spelling tasks).
  151.  
  152.  
  153.    The commercial products do, though, offer clear performance advantages
  154. compared to the non-commercial implementations. Of those tasks completed
  155. successfully, DUFF_AWK turned in the slowest execution times almost
  156. without exception. Its performance in the record-counting and line-
  157. numbering tasks suggests the problem is due to poor disk I/O. Results for
  158. the GNU AWK are of a mixed bag: GAWK turns in the fastest times for
  159. regular expression matching yet at the same time ranks among the slowest
  160. for the word-counting and column-sums tasks.
  161.  
  162.  
  163.    In terms of how the language is implemented by each package, I found
  164. one outright bug and several interesting differences while performing
  165. these benchmarks. [NB: The two implementations from MKS differ only in
  166. execution speed and available storage area; therefore, what is said below
  167. about MKS_AWK applies to MKS_AWKL as well.] The bug involves GAWK's
  168. handling of the metacharacter '+' for regular expression matching. An
  169. earlier version of the column-sums tasks had to be changed because it used
  170. this metacharacter. The differences arise because several areas of the
  171. language are left up to the implementors themselves; they do not indicate
  172. any lack of compliance with the de facto standard of _The AWK Programming
  173. Language_.
  174.  
  175.  
  176.    The most disturbing difference concerns the function printf() in
  177. DUFF_AWK: printf("%d", i) display properly only numbers in the range
  178. [-32768, 32767]. [Strangely enough, this behavior does not occur if the
  179. statement print i is used!] Although it is possible to avoid problematic
  180. results when working with numbers outside this range, I'd certainly like
  181. to see some mention of this and similar limitations in the documentation.
  182.  
  183.  
  184.    Also annoying is the treatment of associative array indices: MKS_AWK
  185. reverses them, both POLY_AWK and DUFF_AWK alphabetize them, and GAWK
  186. rearranges them in some apparently inexplicable fashion. Although the
  187. standard says this is implementation-dependent, it is often desirable to
  188. output the indices in the proper order. With the MKS product, it's just a
  189. hassle; with the others, it's basically impossible.
  190.  
  191.  
  192.    It's also worth mentioning that MKS_AWK, unlike the other three
  193. implementations, does not regard ^Z as an end-of-file marker; whether or
  194. not it should is unclear. This caused some initial consternation when I
  195. was comparing the results of the word-counting task, but otherwise seems
  196. of little import. 
  197.  
  198.  
  199.    Documentation for each package is sparse, but this is because all
  200. conform to the standard closely. Of the four, DUFF_AWK's is by far the
  201. best. It includes not only a discussion of the language but also a brief
  202. tutorial and a ***large*** collection of sample programs. For those new to
  203. AWK this is reason enough to grab a copy of Rob Duff's package. GAWK comes
  204. with a single unix-style man page which focuses primarily on GNU-specific
  205. extensions; additional documentation is available separately. MKS supplies
  206. a copy of _The AWK Programming Language_ and a reference manual covering
  207. not only AWK but several other utilities included with its package. I've
  208. found one mistake (about maximum record size) and another omission (about
  209. placement of temporary files), but overall it's quite adequate. Polytron
  210. also supplies the book and a single README file. The latter mentions first
  211. those examples in the AWK book which don't work because of shortcomings
  212. inherent to MS-DOS, then Polytron/SAGE's own extensions to the language.
  213.  
  214.  
  215.    What conclusions can be made from these comparisons? Well, as the old
  216. adage says, "you don't get something for nothing". That is, choosing a
  217. non-commericial over a commercial implementation will save you money
  218. initially, but every time you use it you're likely to "pay" a price in
  219. terms of slower performance. Whether this is worth $100 depends on _your_
  220. particular situation. On one hand, if you're interested in learning about
  221. AWK, use it only infrequently, or process small files (and don't work in a
  222. commercial environment) you'll probably be quite happy with either Duff's
  223. AWK or GAWK. On the other, if you can justify spending the money, you'll
  224. face a tough choice between the offerings from MKS and Polytron, with
  225. Polytron's product looking slightly better. Nevertheless, all four
  226. implementations are well worth your consideration, and I have no qualms
  227. about recommending them as effective tools for users of DOS-based
  228. computers.
  229.  
  230.  
  231.    Disclaimer: Apart from being a satisfied owner of Mortice Kern System's
  232. AWK and Polytron's PolyShell, I have no direct connection with the
  233. companies or people mentioned above. 
  234.  
  235.  
  236.    If you have any comments about this article, or the AWK language in
  237. general, please get in touch. For those with email access, I can be
  238. reached as GTHEALL@PENNDRLS (BITNET) or GTHEALL@PENNDRLS.UPENN.EDU
  239. (Internet). Otherwise, give me a call at +1 215 898 3419.
  240.  
  241.  
  242.                        Tasks Used in Comparisons
  243.  
  244.  
  245.  
  246. 1. Record-counting task
  247.      END {print NR}
  248.  
  249.  
  250. 2. Word-counting task
  251.      FName != FILENAME {
  252.           if (FName)
  253.                printf("%8.0f%8.0f%8.0f %s\n", lc, wc, cc, FName)
  254.           FName = FILENAME
  255.           cc = wc = lc = 0
  256.      }
  257.  
  258.      {
  259.           cc += length($0) + 1     # don't forget LF!
  260.           wc += NF
  261.           lc ++
  262.      }
  263.  
  264.      END {
  265.           printf("%8.0f%8.0f%8.0f %s\n", lc, wc, cc, FName)
  266.      }
  267.  
  268.  
  269. 3. Line-numbering task
  270.      {print NR ": ", $0}
  271.  
  272.  
  273. 4. Regular-expressions task
  274.      /test|Test|TEST/
  275.  
  276.  
  277. 5. Column-sums task
  278.      $2 ~ /[0-9][0-9]*K$/ {
  279.           sum += $2
  280.      }
  281.  
  282.      END {
  283.           printf("Sum of 2nd column: %16.0fK\n", sum)
  284.      }
  285.  
  286.  
  287. 6. Spelling task
  288.      # List words occurring only once in a document.
  289.      #    A "word" is defined as a sequence of alphanumerics
  290.      #    or underscores.
  291.  
  292.      # Scan thru each line and compute word frequencies.
  293.      # The associative array Words[] holds these frequencies.
  294.      {
  295.           # replace non-alphanumerics with blanks throughout line
  296.           gsub(/[^A-Za-z0-9_]/, " ")
  297.  
  298.           # count how many times each word used
  299.           for (i = 1; i <= NF; i++)          # scan all fields ...
  300.                Words[$i]++                   #    increment word count
  301.      }
  302.  
  303.      # Print out infrequently-used words.
  304.      END {
  305.           for (w in Words)                   # scan over all words ...
  306.                if (Words[w] == 1)            #    if word only appears once ...
  307.                     print w                  #         print it
  308.      }
  309.