Very slow



Create a test file with 20000000 same lines of 50 commas (it will be
1020000000 bytes)
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

Now I have a perl and C program running almost the same code. Perl needs
about 6 minutes to finish while the C version finishes at 20 seconds.
The difference is huge. What can I do for a faster perl version
Following the two programs


--== C ==--


#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>

int main(void) {
char *line = NULL;
char *p = NULL;
int NC = 1;
int n = 0;
size_t len = 0;
ssize_t read;
while (getline(&line, &len, stdin) != -1) {
n = 1;
for (p = line; *p != '\0'; p++) {
if (*p == ',') { n++; }
else if (*p == '\\') { p++; }
}
if (n > NC) {
NC = n;
}
}
if (line) free(line);
printf("%d\n", NC);
return EXIT_SUCCESS;
}


--== Perl ==--



#!/usr/bin/perl

my $NC = 1;
open DATA, '<', '/work/test.txt' or die "$^E\n";

while (<DATA>)
{
my ($i, $n, $Length) = (0, 1, length $_);

while ( $i++ < $Length )
{
my $c = substr $_, $i, 1;
if ( $c eq ',' ) { $n++ } elsif ($c eq '\\') {$i++}
}

$NC = $n if $n > $NC
}

close DATA;
print "Fields $NC\n"


.



Relevant Pages

  • How to best do the following string operations...
    ... I have done in perl using very little code. ... The help I need is just with the above loop. ... int merge_watermark ... char **current_ps_code; ...
    (comp.lang.c)
  • perl to Tcl swig typemaps
    ... I'm kinda struggling trying to build a tcl module from an existing Perl ... In the interface file there's some typemaps defined... ... %typemapint * { ... /* RetrievalRecordBuff is a special construct, to allow to map a char * buf ...
    (comp.lang.tcl)
  • Re: Very slow
    ... I am not very happy with that, because my C collegue is loughing because perl cannot "for" ... Now I have a perl and C program running almost the same code. ... ~% time perl plcommas ...
    (comp.lang.perl.misc)
  • Re: Screen Size
    ... > downloading the source code to perl but was only able to find ... You want a program running on a computer to somehow know the ... display settings of a computer running the browser. ...
    (comp.lang.perl.misc)
  • Re: split a big program into main + (optional) advanced
    ... file containing the definition of that sub, or arrange for it to be ... Perl doesn't go looking for an HTML.pm just because you used ... the same as a stand alone Perl script. ...
    (comp.lang.perl.misc)