Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

incremental string generator - perl

Name: Anonymous 2008-10-09 14:27

real    1m21.853s
user    1m21.070s
sys     0m0.165s


This is on a 1.8ghz c2d. Incremental string generator from charset (999999 passes). Can anyone think of a more efficient way?

my $alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 !?";

my $pass = "a";

for (my $i = 0; $i < 1000000; $i++) {
  $pass = nextPass($pass, $alpha);
}

sub nextPass {
  my ($in, $alpha) = @_;
  my @pass = split //, $in;
  my @set = split //, $alpha;
  my $current = $#pass;
  my $done = 0;
  while (!$done) {
    # is the current character the end of the set?
    if ($pass[$current] eq $set[$#set]) {
      # set the current character to the start of the set
      $pass[$current] = $set[0];
      # is the current character the beginning of the pass?
      if ($current == 0) {
        # add the start of the set to the end of the pass and finish
        push(@pass, $set[0]);
        $done = 1;
      } else {
        $current--;
      }
    } else {
      my $spot = index($alpha, $pass[$current]) + 1;
      $pass[$current] = $set[$spot];
      $done = 1;
    }
  }
  my $out = join "", @pass;
  return $out;
}

Name: Anonymous 2008-10-09 15:27

Bueller?

Name: Anonymous 2008-10-09 15:29

Should have used The Algorithmic Language Scheme.

Name: Anonymous 2008-10-09 16:06

read TAoCP

Name: Anonymous 2008-10-09 16:12

1. don't use perl for c's job
2. don't do a string scan every time you want to know what number you just stored
3. don't convert between strings and arrays on every pass
4. write it in fucking c goddamn

Name: Anonymous 2008-10-09 16:27

Such a waste of precious CPU power should make you sick. Man up and learn ASM.

Name: Anonymous 2008-10-09 17:24

>>6
Seconded!

Name: Anonymous 2008-10-09 18:38

FUck ASM we'll do it in Ruby

Name: Anonymous 2008-10-09 19:08

0,00s user
0,00s system
0,008 total



buffsize equ 10

section .bss
pass: resb buffsize

section .text
        global _start
_start:
        mov byte [pass], 30h
        mov r9, 0
        mov rcx, 999999
nextPass:
        mov r8, r9      ; current
.while:
        cmp byte [pass + r8], 'z'
        jne .else
.if:
        mov byte [pass + r8], 30h
        cmp r8, 0
        jne .over
        mov byte [pass + r8 + 1], 30h
        inc r9
        loop nextPass
        jmp away
.over:
        dec r8
        jmp .while
.else:
        inc byte [pass + r8]
        loop nextPass
away:
        mov rax, 4
        mov rbx, 1
        mov rcx, pass
        mov rdx, buffsize
        int 80h

        mov rax, 1
        xor rbx, rbx
        int 80h

Name: Anonymous 2008-10-09 20:32

>>9
post benchmarks plz

Name: Anonymous 2008-10-09 20:53

>>10
Look at the 3 first lines, idiot.

Name: Anonymous 2008-10-09 21:00

My computer is a wee bit faster than his, so the perl script clocked in at 45 seconds, while mine is 0.008.

45/0.008 = 5625 times faster.

Name: Anonymous 2008-10-09 21:07

Wait, my code includes a syscall to print the output, while his stays silent.

After removing the syscall it's 0.004, so 11250 times faster.

Name: Anonymous 2008-10-09 21:51

>>11
I did read the first 3 lines, but 0,008 is not a number.

Name: Anonymous 2008-10-09 21:57

One word, the forced Europification of the numbers.

Name: Anonymous 2008-10-09 23:01

>>14
In European countries, the comma character is used as the decimal separator, whereas in the US and other countries the period is used.

Name: Anonymous 2008-10-09 23:40

>>9
That's great and all, but you don't really factor in the ``writing-to-a-file'' aspect of the program. I'd imagine including file writing would increase the elapsed time by a lot.

Name: Anonymous 2008-10-09 23:52

>>16
Please stick to the SI.

Name: Anonymous 2008-10-10 1:18

>>16
it's interesting, because in my mother country Croatia, we use both comma and period.

Name: Anonymous 2008-10-10 1:22

>>17
It would also be an utterly ridiculous thing to add.

Name: Anonymous 2008-10-10 1:55

>>19
At random?

Name: Anonymous 2008-10-10 1:56

What is an incremental string generator anyway?

Name: Anonymous 2008-10-10 2:32

OP, seriously, read SICP.

Original:
use Benchmark qw(:all);

my $alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 !?";

my $pass = "a";

timethis (4, sub{
for (my $i = 0; $i < 10000; $i++) {
  $pass = nextPass($pass, $alpha);
}
});

sub nextPass {
  my ($in, $alpha) = @_;
  my @pass = split //, $in;
  my @set = split //, $alpha;
  my $current = $#pass;
  my $done = 0;
  while (!$done) {
    # is the current character the end of the set?
    if ($pass[$current] eq $set[$#set]) {
      # set the current character to the start of the set
      $pass[$current] = $set[0];
      # is the current character the beginning of the pass?
      if ($current == 0) {
        # add the start of the set to the end of the pass and finish
        push(@pass, $set[0]);
        $done = 1;
      } else {
        $current--;
      }
    } else {
      my $spot = index($alpha, $pass[$current]) + 1;
      $pass[$current] = $set[$spot];
      $done = 1;
    }
  }
  my $out = join "", @pass;
  return $out;
}


result: timethis 4:  6 wallclock secs ( 5.34 usr +  0.00 sys =  5.34 CPU) @  0.75/s (n=4)

Modified:
use strict;
use Benchmark qw(:all);

sub make_passer($){
    my @chars=split //,shift;
   
    my $last=$chars[$#chars];
    my %re=map{
        $chars[$_]    => defined $chars[$_+1]?$chars[$_+1]:$chars[0]
    } 0..$#chars;
   
    sub{
        my($line)=@_;
        for(1..length $line){
            for(substr $line,(length $line)-$_,1){
                $_=$chars[0],next if $_ eq $last;
               
                $_=$re{$_};
                return $line;
            }
        }
       
        "$chars[0]$line"
    }
}

my $sub=make_passer "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 !?";
my $val="a";

timethis 4,sub{
    for(0..10000){
        $val=$sub->($val);
    }
}


result: timethis 4:  0 wallclock secs ( 0.25 usr +  0.00 sys =  0.25 CPU) @ 16.00/s (n=4)

Name: Anonymous 2008-10-10 2:35

And, >>9, fuck you, this should work with unicode strings. Go back to seventies with your ascii.

Name: Anonymous 2008-10-10 2:40

>>20
Dictionary generators have a wide application of uses oh God what am I saying. I think I'm starting to lose my mind, but as long as I know I'm losing my mind I can't possibly be losing my mind

Name: Anonymous 2008-10-10 3:10

>>23
Excellent use of the Factory pattern.

Name: Anonymous 2008-10-10 3:26

>>26
アイイ フィイ カイン オ セ アバウウ イ

Name: Anonymous 2008-10-10 5:00

Op, you are a big liar.

sys     0m0.165s

Yeah, sure
You timed something else

Name: Anonymous 2008-10-10 8:43

>>24
It uses UTF-8.

For UCS-2 or UTF-32, just change byte to word and dword, and resb to resw and resd.

Also, it's relatively easy to make it use a character map, I just thought I'd make it OMG OPTIMIZED.

Name: Anonymous 2008-10-10 9:18

>>29
It most definitely does not use utf-8; utf-8 is not some magical encoding that makes everything work. Think before posting next time.

Name: Anonymous 2008-10-10 12:32

>>30
It is UTF-8. It's also ASCII and ISO-6689-*.

Name: Anonymous 2008-10-10 15:19

Which characters that are in unicode and not in ascii can your program produce?

Name: Anonymous 2008-10-10 16:38

>>32
Anything in iso-8859-1.  Or any 255 characters you like, if you define a mapping to them.

Name: Anonymous 2009-03-06 8:27


Uses 57 types and   the logic that   chooses them 2.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List