Well, let's benchmark and see. I created a /etc/passwd file with over 6000 users each named x and some number. Here I search for the user x1, x50, x1000, x3000, and x6000. I test with getpwnam and a regex. Each test is tried 1000 times and the output follows(All tests run on a PII-450 with 384MB RAM running Redhat 9):
#!/usr/bin/perl -w use Benchmark; for $x (1, 50, 1000, 3000, 6000){ timethese (1000,{ "getpwnam${x}" => q { $there=getpwnam("x${x}") }, "regex${x}" => q {open(PASSWD,"/etc/passwd"); while(<PASSWD>) { if (/^x${x}:/){$alsothere=1}; last if $alsothere == 1; } close PASSWD;}, }); } Benchmark: timing 1000 iterations of getpwnam1, regex1... getpwnam1: 0 wallclock secs ( 0.13 usr + 0.05 sys = 0.18 CPU) @ 55 +55.56/s (n=1000) (warning: too few iterations for a reliable count) regex1: 0 wallclock secs ( 0.10 usr + 0.03 sys = 0.13 CPU) @ 76 +92.31/s (n=1000) (warning: too few iterations for a reliable count) Benchmark: timing 1000 iterations of getpwnam50, regex50... getpwnam50: 1 wallclock secs ( 0.29 usr + 0.02 sys = 0.31 CPU) @ 32 +25.81/s (n=1000) (warning: too few iterations for a reliable count) regex50: 0 wallclock secs ( 0.08 usr + 0.05 sys = 0.13 CPU) @ 76 +92.31/s (n=1000) (warning: too few iterations for a reliable count) Benchmark: timing 1000 iterations of getpwnam1000, regex1000... getpwnam1000: 2 wallclock secs ( 2.36 usr + 0.24 sys = 2.60 CPU) @ +384.62/s (n=1000) regex1000: 0 wallclock secs ( 0.10 usr + 0.04 sys = 0.14 CPU) @ 71 +42.86/s (n=1000) (warning: too few iterations for a reliable count) Benchmark: timing 1000 iterations of getpwnam3000, regex3000... getpwnam3000: 8 wallclock secs ( 7.26 usr + 0.43 sys = 7.69 CPU) @ +130.04/s (n=1000) regex3000: 1 wallclock secs ( 0.12 usr + 0.05 sys = 0.17 CPU) @ 58 +82.35/s (n=1000) (warning: too few iterations for a reliable count) Benchmark: timing 1000 iterations of getpwnam6000, regex6000... getpwnam6000: 16 wallclock secs (14.73 usr + 0.67 sys = 15.40 CPU) @ +64.94/s (n=1000) regex6000: 0 wallclock secs ( 0.13 usr + 0.06 sys = 0.19 CPU) @ 52 +63.16/s (n=1000) (warning: too few iterations for a reliable count)
Suprisingly the regex wins hands down. Not too shabby. I don't think you need to worry about your script being slow at all.
HTH
In reply to Re: User Existance?
by pzbagel
in thread User Existance?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |