String case (was: Re: Accepts invalid?)

Salvatore Filippone salvatore.filippone@uniroma2.it
Sun Aug 31 21:22:00 GMT 2008


As for converting case in Fortran, and the example given by Daniel
Franke, there are at least three other approaches:

1. roll your own version of INDEX exploiting the fact that the
lowercase/uppercase strings are sorted;
2. Use a giant SELECT statement
    SELECT CASE(in(i:i))
    CASE('A')
      out(i:i) = 'a'
 .....
    CASE DEFAULT
      out(i:i) = in(i:i)
3. Build a table such that
   tolowertab(i) = achar(i)
   everywhere, except for 
   tolowertab(ichar('A')) = 'a'
    etc. 

The attached code read a bunch (128K) lines of 80 characters from a
file, then applies in succession the four algorithms to substrings of
the  lines, with growing lengths. The input file is a mixture of Fortran
90, C and TeX source files. 

The results with gfortran 4.3.1 -O3 -march=native on my Core 2 duo say
that the tabular approach wins hands down for long strings, but the
binary search index is also good for short strings. The SELECT is really
bad. 
YMMV. 

Note that this does NOT take into account accented letters, something
that my native language would want, but then, in that case, you're
probably better off with some other tools.... 

 Timings for tolower on       131072  strings.
 Length            Index   Srt. Index       Select          Tab
       1      4.3402E-02   2.2024E-02   7.0476E-02   2.1090E-02
       2      6.6103E-02   2.4197E-02   1.1923E-01   2.2238E-02
       3      8.8907E-02   2.7437E-02   1.6985E-01   2.2252E-02
       4      1.1021E-01   2.8809E-02   2.1861E-01   2.1893E-02
       5      1.3308E-01   3.0973E-02   2.6929E-01   2.2461E-02
       6      1.5539E-01   3.2335E-02   3.1678E-01   2.2976E-02
       7      1.7803E-01   3.4322E-02   3.6537E-01   2.3674E-02
       8      1.9977E-01   4.0478E-02   3.7977E-01   2.3372E-02
       9      2.2328E-01   4.2629E-02   4.1650E-01   2.4409E-02
      10      2.4483E-01   4.6903E-02   4.5040E-01   2.4451E-02
      15      3.5693E-01   5.9450E-02   6.3592E-01   2.6715E-02
      20      4.6643E-01   7.2292E-02   8.3001E-01   2.7864E-02
      25      5.7704E-01   8.5622E-02   1.0197E+00   3.0456E-02
      30      6.8782E-01   9.8541E-02   1.2019E+00   3.2168E-02
      35      7.9851E-01   1.1138E-01   1.3946E+00   3.4454E-02
      40      9.0826E-01   1.2303E-01   1.5804E+00   3.5742E-02
      50      1.1306E+00   1.4602E-01   1.9465E+00   3.9895E-02
      60      1.3469E+00   1.6510E-01   2.2979E+00   4.1917E-02
      70      1.5805E+00   1.9106E-01   2.6358E+00   5.1041E-02
      80      1.8013E+00   2.0884E-01   2.9793E+00   5.4562E-02
 Timings for toupper on       131072  strings.
 Length            Index   Srt. Index       Select          Tab
       1      3.1158E-02   2.2613E-02   5.9888E-02   2.0703E-02
       2      4.7943E-02   2.3659E-02   8.5656E-02   2.1608E-02
       3      5.8065E-02   2.8629E-02   1.1974E-01   2.1970E-02
       4      7.8570E-02   2.5435E-02   1.2231E-01   2.1510E-02
       5      8.8967E-02   2.9107E-02   1.6045E-01   2.3129E-02
       6      1.1268E-01   3.3980E-02   1.8520E-01   2.2927E-02
       7      1.2477E-01   3.7376E-02   2.1915E-01   2.3499E-02
       8      1.4695E-01   4.1472E-02   2.5257E-01   2.5582E-02
       9      1.6963E-01   4.7824E-02   2.8357E-01   2.4178E-02
      10      1.9015E-01   5.3079E-02   3.1403E-01   2.4218E-02
      15      2.8256E-01   7.3696E-02   4.6449E-01   2.6709E-02
      20      3.7452E-01   9.1570E-02   6.1465E-01   2.7942E-02
      25      4.6918E-01   1.1024E-01   7.6694E-01   3.0342E-02
      30      5.6488E-01   1.2770E-01   9.1970E-01   3.2273E-02
      35      6.6174E-01   1.4425E-01   1.0723E+00   3.4518E-02
      40      7.5857E-01   1.5909E-01   1.2220E+00   3.5764E-02
      50      9.5825E-01   1.8787E-01   1.5264E+00   3.9844E-02
      60      1.1596E+00   2.1147E-01   1.8312E+00   4.1755E-02
      70      1.3733E+00   2.3940E-01   2.1473E+00   5.1048E-02
      80      1.5896E+00   2.5867E-01   2.4538E+00   5.4585E-02




Feel free to reuse

Salvatore


   
   
-------------- next part --------------
A non-text attachment was scrubbed...
Name: try_string.f90
Type: text/x-fortran
Size: 10292 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/fortran/attachments/20080831/1392bbf1/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: timers.c
Type: text/x-csrc
Size: 1668 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/fortran/attachments/20080831/1392bbf1/attachment-0001.bin>


More information about the Fortran mailing list