Please create an account to participate in the Slashdot moderation system

 



Forgot your password?
typodupeerror
×
Perl

Journal lithron's Journal: [perl] Replacing String::Random

So I wanted to see what the quickest way to produce a 8 character long password that you can type with a standard keyboard was.. so I decided to use String::Random 0.20 from CPAN.  And I found it to be a bit slower than I thought it should be.  So I wrote my own litle 3 line procedure that does the same thing, and I benchmarked the two.

Below you'll see the script, and the benchmarking.  I was shocked at how much quicker my version is.  Corrections to my test script are welcome, because I don't see any bugs, and I don't really believe the results..

use strict;
use String::Random qw(random_string);
my $inc = 0;
my $result = "";
sub createrandom {
  my $re = '';
  for (1..8) { $re = $re . chr(rand(93) + 34); }
  return $re;
}
while ($inc < 10000) {
  $inc++;
  $result = random_string('........');
  $result = createrandom();
}

root@head ~/distmd5
$ perl -d:DProf test.pl

root@head ~/distmd5
$ dprofpp
Total Elapsed Time = 1.272819 Seconds
  User+System Time = 1.142819 Seconds
Exclusive Times
%Time ExclSec CumulS #Calls sec/call Csec/c  Name
66.5   0.760  0.760  10000   0.0001 0.0001  String::Random::randpattern
23.7   0.271  1.261  10000   0.0000 0.0001  String::Random::random_string
20.2   0.231  0.231  10000   0.0000 0.0000  main::createrandom
20.1   0.230  0.230  10000   0.0000 0.0000  String::Random::new
2.63   0.030  0.050      2   0.0150 0.0249  main::BEGIN
0.88   0.010  0.010      3   0.0033 0.0033  vars::BEGIN
0.88   0.010  0.020      6   0.0017 0.0033  String::Random::BEGIN
0.00       - -0.000      1        -      -  warnings::BEGIN
0.00       - -0.000      2        -      -  strict::bits
0.00       - -0.000      2        -      -  warnings::register::mkMask
0.00       - -0.000      2        -      -  Exporter::import
0.00       - -0.000      3        -      -  vars::import
0.00       - -0.000      1        -      -  warnings::register::import
0.00       - -0.000      3        -      -  strict::import
This discussion has been archived. No new comments can be posted.

[perl] Replacing String::Random

Comments Filter:

"I prefer the blunted cudgels of the followers of the Serpent God." -- Sean Doran the Younger

Working...