in reply to Python regex faster than Perl?
Recently, I tried diffing two large files only for the UNIX diff command to choke the OS. That's because the diff utility slurps both files, requiring 2x memory consumption.
I thought to provide chunking variants and measure the time taken for a 99 MB file.
Perl
#!/usr/bin/env perl #use v5.20; #use feature qw(signatures); #no warnings qw(experimental::signatures); use v5.36; use autodie; exit 1 if not @ARGV; sub read_file ($fh, $chunk_size=65536) { # Return the next chunk, including to the end of line. read($fh, my $chunk, $chunk_size); if (length($chunk) && substr($chunk, -1) ne "\n") { return $chunk if eof($fh); $chunk .= readline($fh); } return $chunk; } my $mul_pattern = 'mul\(\d{1,3},\d{1,3}\)'; my $filename = shift; my $count = 0; if (open(my $fh, '<', $filename)) { while (length(my $chunk = read_file($fh))) { $count += () = $chunk =~ m/$mul_pattern/g; } } print "Found $count matches.\n";
Python
#!/usr/bin/env python import re, sys if len(sys.argv) < 2: sys.exit(1) def read_file (file, chunk_size=65536): """ Lazy function generator to read a file in chunks, including to the end of line. """ while True: chunk = file.read(chunk_size) if not chunk: break if not chunk.endswith('\n'): chunk += file.readline() yield chunk mul_re = re.compile(r"mul\(\d{1,3},\d{1,3}\)") filename = sys.argv[1] count = 0 try: with open (filename, "r") as file: for chunk in read_file(file): count += len(mul_re.findall(chunk)) except Exception as e: print(e, file=sys.stderr) sys.exit(1) print(f"Found {count} matches.")
Results
Perl 0.463s Found 200246 matches. Python 0.250s Found 200246 matches.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Python regex faster than Perl? - Chunking 1 GB
by marioroy (Prior) on Mar 27, 2025 at 07:53 UTC | |
by marioroy (Prior) on Mar 27, 2025 at 08:31 UTC | |
by marioroy (Prior) on Mar 27, 2025 at 14:19 UTC |