From: Holger Kiehl <Holger.Kiehl@dwd.de>
To: Luciano Moreira - igLnx <lucianolnx@ig.com.br>
Cc: "Alphex K." <alphex@crew.org.ru>, linux-c-programming@vger.kernel.org
Subject: Re: Implementing a file counter (like "ls | wc")
Date: Wed, 7 Apr 2004 15:34:18 +0000 (GMT) [thread overview]
Message-ID: <Pine.LNX.4.58.0404071523290.32748@praktifix.dwd.de> (raw)
In-Reply-To: <4074148E.9060205@ig.com.br>
On Wed, 7 Apr 2004, Luciano Moreira - igLnx wrote:
> -------------- THE CODE HAVE THIS STRUCTURE:
> while ((pFile=readdir(pDir))!=NULL) {
> sprintf(szBuf, "%s/%s", pPath, pFile->d_name);
> stat(szBuf, &statFile);
> if (S_ISDIR(statFile.st_mode)) /// LOOK THAT: We don't use recursive
> searching, we count only files at current directory excluding others
> directories.
> continue;
>
> /* Filtering */
> if (nNeedFilter) {
> //// I DONT HAVE THE CODE OF FILTERING NOW
> //// BUT I CAN SEND IT LATER IF NECESSARY
> }
>
> }
> -------------- CODE FINISH HERE
>
Don't use sprintf(), its very expansive. Before the while loop put a pointer
after path and / and then strcpy(ptr, pFile->d_name)
stat() is _very_ epansive! It means physical IO and fills up a structure
with things you just don't need. If you really do need to filter out
directories from your result do the stat after it has passed the filter.
> Does have another mechanism to filter without using of strcmp() / memcmp() ?
> How if we dont know the size of extension (.c, .cpp, .teste, .longextension,
> and so on). ?
>
Compare them yourself with a pointer byte for byte. But the speed gain
will not be so high as when you leave away the stat() call.
Holger
next prev parent reply other threads:[~2004-04-07 15:34 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-04-07 14:35 Implementing a file counter (like "ls | wc") Alphex K.
2004-04-07 14:47 ` Luciano Moreira - igLnx
2004-04-07 15:34 ` Holger Kiehl [this message]
2004-04-13 12:52 ` Steven Smith
-- strict thread matches above, loose matches on Subject: below --
2004-04-07 14:27 Luciano Moreira - igLnx
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.0404071523290.32748@praktifix.dwd.de \
--to=holger.kiehl@dwd.de \
--cc=alphex@crew.org.ru \
--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).