cmilfo has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl open(GRADES, "grades") or die "Can't open grades: $!\n"; while ($line = <GRADES>) { ($student, $grade) = split(" ", $line); $grades{$student} .= $grade . " "; } foreach $student (sort keys %grades) { $scores = 0; $total = 0; @grades = split(" ", $grades{$student}); foreach $grade (@grades) { $total += $grade; $scores++; } $average = $total / $scores; print "$student: $grades{$student}\tAverage: $average\n"; }
#!/usr/bin/perl open(GRADES, "grades") or die "Can't open grades: $!\n"; while ($line = <GRADES>) { ($student, $grade) = split(" ", $line); $grades{$student} .= $grade . " "; } foreach $student (sort keys %grades) { $scores = 0; $total = 0; foreach $grade (split(" ", $grades{$student})) { $total += $grade; $scores++; } $average = $total / $scores; print "$student: $grades{$student}\tAverage: $average\n"; }
Benchmark: timing 100000 iterations of from_book, from_book2... from_book: 60 wallclock secs (41.60 usr + 4.34 sys = 45.94 CPU) @ 21 +76.75/s (n=100000) from_book2: 55 wallclock secs (39.09 usr + 3.81 sys = 42.90 CPU) @ 23 +31.00/s (n=100000) Rate from_book from_book2 from_book 2177/s -- -7% from_book2 2331/s 7% --
Benchmark: timing 1000 iterations of from_book, from_book2... from_book: 278 wallclock secs (211.20 usr + 0.68 sys = 211.88 CPU) @ + 4.72/s (n=1000) from_book2: 261 wallclock secs (200.35 usr + 0.56 sys = 200.91 CPU) @ + 4.98/s (n=1000) Rate from_book from_book2 from_book 4.72/s -- -5% from_book2 4.98/s 5% --
Edit Masem 2002-01-23 - Changed title from "Page 18 of the Camel"
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Page 18 of the Camel
by shotgunefx (Parson) on Jan 22, 2002 at 14:02 UTC | |
by Aristotle (Chancellor) on Jan 22, 2002 at 17:43 UTC | |
|
Re: Page 18 of the Camel
by belg4mit (Prior) on Jan 22, 2002 at 12:36 UTC | |
by cmilfo (Hermit) on Jan 22, 2002 at 12:47 UTC | |
by belg4mit (Prior) on Jan 22, 2002 at 13:05 UTC |