本帖最后由 523066680 于 2019-4-13 21:48 编辑
写了一个很冗长的版本,按之前的测试数值 4416位数 + 2496位数,耗时200ms
顺带用其他脚本写了一个效率和准确性测试, | use Time::HiRes qw/time/; | | use bigint 'a'=>9000; | | STDOUT->autoflush(1); | | srand(1); | | for (1..50) { check() } | | | | sub check | | { | | my $a = int(rand(9))+1 .join "", map { int(rand(10)) } ( 1 .. rand(4100) ); | | my $b = int(rand(9))+1 .join "", map { int(rand(10)) } ( 1 .. rand(4100) ); | | | | if ( length($b) > length($a) ) { ($a, $b) = ($b, $a); } | | my $ta = time(); | | my $res = `plus_vec4.bat $a $b`; | | my $dt = time()-$ta; | | my $check = (0+$a)+$b; | | $res =~s/\r?\n$//; | | if ( $res eq $check ) { | | printf "%4d, %4d, correct, %.2f\n", length($a), length($b), $dt; | | } else { | | printf "%4d, %4d, wrong, %.2f\n", length($a), length($b), $dt; | | } | | }COPY |
左边是两个数值的长度,最右是耗时 | 3327, 1864, correct, 0.17s | | 2627, 2125, correct, 0.13s | | 4067, 1529, correct, 0.20s | | 3224, 558, correct, 0.16s | | 1716, 7, correct, 0.08s | | 4056, 3309, correct, 0.22s | | 2429, 566, correct, 0.12s | | 717, 443, correct, 0.04s | | 3880, 2849, correct, 0.20s | | 2459, 95, correct, 0.12s | | 1212, 998, correct, 0.06s | | 3908, 1325, correct, 0.19s | | 3219, 392, correct, 0.15s | | 3483, 1089, correct, 0.17s | | 137, 9, correct, 0.04s | | 2306, 2297, correct, 0.12s | | 2308, 1083, correct, 0.12s | | 3207, 1677, correct, 0.16s | | 1919, 1862, correct, 0.09s | | 1071, 14, correct, 0.05s | | 3823, 3439, correct, 0.20s | | 3750, 448, correct, 0.18s | | 2067, 1724, correct, 0.10s | | 1565, 1469, correct, 0.07s | | 2388, 2052, correct, 0.12s | | 2271, 1209, correct, 0.11s | | 2642, 403, correct, 0.12s | | 2352, 1198, correct, 0.11s | | 4087, 2076, correct, 0.21s | | 1917, 349, correct, 0.08s | | 3579, 882, correct, 0.17s | | 2276, 1315, correct, 0.11s | | 3243, 2225, correct, 0.17s | | 735, 543, correct, 0.04s | | 3111, 2193, correct, 0.16s | | 2992, 1849, correct, 0.15s | | 3672, 3562, correct, 0.20s | | 3729, 1593, correct, 0.18s | | 4000, 3199, correct, 0.22s | | 3888, 2476, correct, 0.20s | | 2899, 1969, correct, 0.15s | | 2548, 1586, correct, 0.13s | | 3407, 383, correct, 0.16s | | 2608, 210, correct, 0.13s | | 4022, 792, correct, 0.19s | | 4022, 1176, correct, 0.20s | | 3376, 2227, correct, 0.17s | | 1978, 84, correct, 0.09s | | 1510, 304, correct, 0.07s | | 2602, 1592, correct, 0.13sCOPY |
最后,随手写一个 50000 位随机数相加,我写的很冗长  | use bigint; | | srand(1); | | my $gener = sub { int(rand(9))+1 .join "", map {int(rand(10))} (2 .. shift) }; | | my $a = $gener->(50000); | | my $b = $gener->(50000); | | print 0+$a+$b;COPY |
|