home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / c / 20348 < prev    next >
Encoding:
Text File  |  1993-01-28  |  1.8 KB  |  63 lines

  1. Path: sparky!uunet!ukma!gatech!prism!xray.gatech.edu!cc100aa
  2. From: cc100aa@xray.gatech.edu (Ray Spalding)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Performance question
  5. Message-ID: <82327@hydra.gatech.EDU>
  6. Date: 29 Jan 93 02:08:23 GMT
  7. References: <1993Jan28.151201.12690@ctp.com>
  8. Sender: news@prism.gatech.EDU
  9. Organization: Georgia Institute of Technology
  10. Lines: 51
  11.  
  12. In article <1993Jan28.151201.12690@ctp.com> mvarg@ctp.com (Michael Vargo) writes:
  13. >If I had a string with a format like "data1|data2|data3|data4|data5|...etc."
  14. >and it's **HUGE**, what do you think the FASTEST way is to extract the
  15. >data and put it into an array of strings....Any help is appreciated....
  16.  
  17. The fastest way is of course subject to change depending on your
  18. machine, compiler, library, optimization options, etc.  However,
  19. my advice would be to use library functions (such as strchr())
  20. where possible rather than stepping through the string char-by-char.
  21. This is because library functions may be faster than any C code
  22. could be, perhaps, for example, by being implemented in assembler.
  23. Also, avoid copying if possible, or use one strcpy() on the whole
  24. original string rather than the individual substrings.
  25.  
  26. A sample:
  27.  
  28. #include    <stdio.h>
  29. #include    <string.h>
  30.  
  31. #define    MAXSTR    32
  32.  
  33. int
  34. main()
  35. {
  36.     static char data[] = "data1|data2|data3|data4|data5";
  37.     char *datptr[MAXSTR];
  38.     char *sp;
  39.     char *cp;
  40.     int nstr;
  41.     int i;
  42.  
  43.     sp = data;
  44.     nstr = 0;
  45.     for (;;) {
  46.         if (nstr >= MAXSTR) {
  47.             printf("Too many strings\n");
  48.             break;
  49.         }
  50.         datptr[nstr++] = sp;
  51.         cp = strchr(sp,'|');
  52.         if (!cp) break;
  53.         *cp = '\0';
  54.         sp = cp + 1;
  55.     }
  56.     for (i = 0; i < nstr; ++i) printf("%d: %s\n",i,datptr[i]);
  57.     return 0;
  58. }
  59. -- 
  60. Ray Spalding, Office of Information Technology
  61. Georgia Institute of Technology, Atlanta Georgia, 30332-0715
  62. Internet: ray.spalding@oit.gatech.edu (NeXT Mail accepted)
  63.