This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: get rid of getpwent? (Was: cygwin-1.7.28 getpwent header declaration changes ?)


On 2/7/2014 3:09 PM, Warren Young wrote:
This takes 7.1 seconds on my system, with a 12-line /etc/passwd file:

      #include <pwd.h>
      #include <stdio.h>
      #include <stdlib.h>

      int main(int argc, const char* argv[])
      {
          int i;
          const char* user = argv[1];

          if (!user) {
              printf("usage: %s username\n", argv[0]);
              exit(1);
          }

          for (i = 0; i < 1000000; ++i) {
              struct passwd* pw = getpwnam(user);
              if (!pw) {
                  printf("User %s doesn't exist!\n", user);
                  exit(2);
              }
              else if (i == 0) {
                  printf("User %s is UID %d\n", user, pw->pw_uid);
              }
          }
      }

So, each getpwnam() call takes 7.1 microseconds on average.

I think you forgot to put an "exit(0);" after the last printf().  Without
it, you're checking for the same user a million times, which is certainly
going to take a little time. ;-)

--
Larry

_____________________________________________________________________

A: Yes.
> Q: Are you sure?
>> A: Because it reverses the logical flow of conversation.
>>> Q: Why is top posting annoying in email?

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]