home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!ukma!gatech!prism!xray.gatech.edu!cc100aa
- From: cc100aa@xray.gatech.edu (Ray Spalding)
- Newsgroups: comp.lang.c
- Subject: Re: Performance question
- Message-ID: <82327@hydra.gatech.EDU>
- Date: 29 Jan 93 02:08:23 GMT
- References: <1993Jan28.151201.12690@ctp.com>
- Sender: news@prism.gatech.EDU
- Organization: Georgia Institute of Technology
- Lines: 51
-
- In article <1993Jan28.151201.12690@ctp.com> mvarg@ctp.com (Michael Vargo) writes:
- >If I had a string with a format like "data1|data2|data3|data4|data5|...etc."
- >and it's **HUGE**, what do you think the FASTEST way is to extract the
- >data and put it into an array of strings....Any help is appreciated....
-
- The fastest way is of course subject to change depending on your
- machine, compiler, library, optimization options, etc. However,
- my advice would be to use library functions (such as strchr())
- where possible rather than stepping through the string char-by-char.
- This is because library functions may be faster than any C code
- could be, perhaps, for example, by being implemented in assembler.
- Also, avoid copying if possible, or use one strcpy() on the whole
- original string rather than the individual substrings.
-
- A sample:
-
- #include <stdio.h>
- #include <string.h>
-
- #define MAXSTR 32
-
- int
- main()
- {
- static char data[] = "data1|data2|data3|data4|data5";
- char *datptr[MAXSTR];
- char *sp;
- char *cp;
- int nstr;
- int i;
-
- sp = data;
- nstr = 0;
- for (;;) {
- if (nstr >= MAXSTR) {
- printf("Too many strings\n");
- break;
- }
- datptr[nstr++] = sp;
- cp = strchr(sp,'|');
- if (!cp) break;
- *cp = '\0';
- sp = cp + 1;
- }
- for (i = 0; i < nstr; ++i) printf("%d: %s\n",i,datptr[i]);
- return 0;
- }
- --
- Ray Spalding, Office of Information Technology
- Georgia Institute of Technology, Atlanta Georgia, 30332-0715
- Internet: ray.spalding@oit.gatech.edu (NeXT Mail accepted)
-