linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Implementing a file counter (like "ls | wc")
@ 2004-04-07 14:27 Luciano Moreira - igLnx
  0 siblings, 0 replies; 5+ messages in thread
From: Luciano Moreira - igLnx @ 2004-04-07 14:27 UTC (permalink / raw)
  To: linux-c-programming

We implemented a program that use "opendir()", "readdir()", and 
"closedir()" to scan a directory and count the files contained into it.

But, it program have taking more time than "ls | wc" to count files.

Does have any other way to count files faster?

PS: Our program needs sometime to filter files by extension (like: *.c, 
*.cpp, and so on. Thus we use strcmp() to compare last letters of both 
strings).

Luciano


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Implementing a file counter (like "ls | wc")
@ 2004-04-07 14:35 Alphex K.
  2004-04-07 14:47 ` Luciano Moreira - igLnx
  0 siblings, 1 reply; 5+ messages in thread
From: Alphex K. @ 2004-04-07 14:35 UTC (permalink / raw)
  To: lucianolnx; +Cc: linux-c-programming

On Wed, 07 Apr 2004 11:27:02 -0300
Luciano Moreira - igLnx <lucianolnx@ig.com.br> wrote:

> We implemented a program that use "opendir()", "readdir()", and 
> "closedir()" to scan a directory and count the files contained into it.
How U use this functions?
But I'm think that it's not good method
> 
> But, it program have taking more time than "ls | wc" to count files.
> 
> Does have any other way to count files faster?
> 
> PS: Our program needs sometime to filter files by extension (like: *.c, 
> *.cpp, and so on. Thus we use strcmp() to compare last letters of both 
> strings).
If U want a more speed don't use strcmp or strncmp functions.
> 
> Luciano
> 
U use a C++ ? or C ?
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html


---===---
Alphex Kaanoken
Senior developer of Crew IT research labs
web: http://crew.org.ru
mailto:Alphex@Crew.Org.RU


---===---
Alphex Kaanoken
Senior developer of Crew IT research labs
web: http://crew.org.ru
mailto:Alphex@Crew.Org.RU

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Implementing a file counter (like "ls | wc")
  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
  2004-04-13 12:52   ` Steven Smith
  0 siblings, 2 replies; 5+ messages in thread
From: Luciano Moreira - igLnx @ 2004-04-07 14:47 UTC (permalink / raw)
  To: Alphex K.; +Cc: linux-c-programming

-------------- 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

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). ?

Luciano

Alphex K. wrote:

>On Wed, 07 Apr 2004 11:27:02 -0300
>Luciano Moreira - igLnx <lucianolnx@ig.com.br> wrote:
>
>  
>
>>We implemented a program that use "opendir()", "readdir()", and 
>>"closedir()" to scan a directory and count the files contained into it.
>>    
>>
>How U use this functions?
>But I'm think that it's not good method
>  
>
>>But, it program have taking more time than "ls | wc" to count files.
>>
>>Does have any other way to count files faster?
>>
>>PS: Our program needs sometime to filter files by extension (like: *.c, 
>>*.cpp, and so on. Thus we use strcmp() to compare last letters of both 
>>strings).
>>    
>>
>If U want a more speed don't use strcmp or strncmp functions.
>  
>
>>Luciano
>>
>>    
>>
>U use a C++ ? or C ?
>  
>
>>-
>>To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
>>the body of a message to majordomo@vger.kernel.org
>>More majordomo info at  http://vger.kernel.org/majordomo-info.html
>>    
>>
>
>
>---===---
>Alphex Kaanoken
>Senior developer of Crew IT research labs
>web: http://crew.org.ru
>mailto:Alphex@Crew.Org.RU
>
>
>---===---
>Alphex Kaanoken
>Senior developer of Crew IT research labs
>web: http://crew.org.ru
>mailto:Alphex@Crew.Org.RU
>
>
>  
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Implementing a file counter (like "ls | wc")
  2004-04-07 14:47 ` Luciano Moreira - igLnx
@ 2004-04-07 15:34   ` Holger Kiehl
  2004-04-13 12:52   ` Steven Smith
  1 sibling, 0 replies; 5+ messages in thread
From: Holger Kiehl @ 2004-04-07 15:34 UTC (permalink / raw)
  To: Luciano Moreira - igLnx; +Cc: Alphex K., linux-c-programming

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Implementing a file counter (like "ls | wc")
  2004-04-07 14:47 ` Luciano Moreira - igLnx
  2004-04-07 15:34   ` Holger Kiehl
@ 2004-04-13 12:52   ` Steven Smith
  1 sibling, 0 replies; 5+ messages in thread
From: Steven Smith @ 2004-04-13 12:52 UTC (permalink / raw)
  To: Luciano Moreira - igLnx; +Cc: linux-c-programming

[-- Attachment #1: Type: text/plain, Size: 550 bytes --]

> while ((pFile=readdir(pDir))!=NULL) {
>      sprintf(szBuf, "%s/%s", pPath, pFile->d_name);
>      stat(szBuf, &statFile);
>      if (S_ISDIR(statFile.st_mode))
>         continue;
If the only reason you're statting the files is to check whether
they're directories, then you can save yourself a system call per
file:

while ((pFile=readdir(pDir))!=NULL) {
     if (pFile->d_type == DT_DIR)
        continue;

This'll probably be quite a bit quicker, and may also save you needing
to call sprintf(), which can be somewhat expensive.

Steven Smith.

[-- Attachment #2: Type: application/pgp-signature, Size: 187 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2004-04-13 12:52 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2004-04-13 12:52   ` Steven Smith
  -- strict thread matches above, loose matches on Subject: below --
2004-04-07 14:27 Luciano Moreira - igLnx

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).