From: Holger Kiehl <Holger.Kiehl@dwd.de>
To: "John T. Williams" <jowillia@vt.edu>
Cc: Luciano Moreira - igLnx <lucianolnx@ig.com.br>,
linux-c-programming@vger.kernel.org
Subject: Re: [Fwd: Re: Implementing a file counter (like "ls | wc")]
Date: Wed, 7 Apr 2004 22:29:31 +0000 (GMT) [thread overview]
Message-ID: <Pine.LNX.4.58.0404072202460.31808@praktifix.dwd.de> (raw)
In-Reply-To: <1081356842.18677.23.camel@Marx.fesnel.no-ip.org>
On Wed, 7 Apr 2004, John T. Williams wrote:
> You should read the GNU source code for ls, they actually filter out
> directories in a quite interesting maner.
>
> instead of using stat which is quite expensive, they try to open each
> file as a directory, which is expensive, but not as expensive as stat.
> If a file opens as a directory, then it is, and they treat it as one,
> (in your case closing it and ignoring it); else if it fails to open as a
> directory, they then use stat to get information about it to print;
>
Indeed this is a good idea, but even then I would only go through this
code path after the filtering. Any system call is always more expensive
then the filtering.
> a simple counter could look something like (my code is paraphrasing
> parts of the GNU code, but is not a copy) Also treat it as Sudo code
> that just looks a lot like C as it is untested (not even compiled) and
> is only trying to make the point.
>
Another minor improvement would be as stated earlier to put the strncpy()
outside the while loop:
> ___________________________________________________
> int count = 0;
> struct dirent dir_entry;
> DIR* directory, test;
> char buff[512];
char *ptr
>
>
> directory = opendir('/etc');
> /* check that it opened*/
> ...
>
strcpy(buff, '/etc/');
ptr = buff + 5;
> while( dir_entry = readdir(directory) ) {
if (filter fits)
{
strcpy(ptr, dir_entry->d_name);
> if( test = opendir( buff ) ) {
> /* gets here the its a directory */
> closedir(test);
> } else {
> /* gets here not a directory */
> /* code to test if its a regular file */
> count++;
> }
}
> }
>
> closedir(directory);
>
If you have a directory with many files this might make a minor difference.
Also note that we now have a strcpy() and not a strcat() in the loop.
A strcat() will always have to find the end of the string first before
it can do its job.
But most important always try to avoid system calls!
Holger
next prev parent reply other threads:[~2004-04-07 22:29 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-04-07 16:50 [Fwd: Re: Implementing a file counter (like "ls | wc")] Luciano Moreira - igLnx
2004-04-07 16:54 ` John T. Williams
2004-04-07 22:29 ` Holger Kiehl [this message]
2004-04-08 0:06 ` A. Murat Eren
2004-04-08 1:01 ` John T. Williams
2004-04-08 4:39 ` Glynn Clements
2004-04-08 8:05 ` A. Murat Eren
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Pine.LNX.4.58.0404072202460.31808@praktifix.dwd.de \
--to=holger.kiehl@dwd.de \
--cc=jowillia@vt.edu \
--cc=linux-c-programming@vger.kernel.org \
--cc=lucianolnx@ig.com.br \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).