[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
本帖最后由 523066680 于 2019-4-12 10:17 编辑

测试效率和 happy886rr 的大数加法耗时几乎一样。从代码上看,思路都是以更大的基数进行分段处理。
http://www.bathome.net/viewthrea ... mp;page=1#pid195561
  1. set num_a=456456521000210000000000000000874112115674511111111111111110019999999
  2. set num_b=923451344221111111111111111000000000001
  3. for /l %%a in (1,1,6) do set num_a=!num_a!!num_a!
  4. for /l %%a in (1,1,6) do set num_b=!num_b!!num_b!
复制代码
happy代码测试
  1. @echo off&setlocal enabledelayedexpansion
  2. title By Happy
  3. REM 大数长度
  4. set "MAX=5000" ^位
  5. REM 分割大小
  6. set "K=8"      ^位
  7. set /a CYC=MAX/K
  8. :MAIN
  9. cls
  10. set num_a=456456521000210000000000000000874112115674511111111111111110019999999
  11. set num_b=923451344221111111111111111000000000001
  12. for /l %%a in (1,1,6) do set num_a=!num_a!!num_a!
  13. for /l %%a in (1,1,6) do set num_b=!num_b!!num_b!
  14. set A=%num_a%
  15. set B=%num_b%
  16. REM 优化字符
  17. setlocal
  18. CALL :POINT !A! A N1
  19. CALL :POINT !B! B N2
  20. set /a B1=N1*K
  21. set /a B2=N2*K
  22. echo ======================================
  23. echo 被加数信息:  预估!B1!位
  24. echo   加数信息:  预估!B2!位
  25. echo ======================================
  26. echo 计算结果
  27. REM 加法核心
  28. if !N1! gtr !N2! (set RM=!N1! &set dx=B) else (set RM=!N2! &set dx=A)
  29. for /l %%i in (1 1 !RM!) do (
  30. if not defined !dx![%%i] (set "!dx![%%i]=00000000")
  31. set /a sum=1!A[%%i]!+1!B[%%i]!+sum
  32. set S=!sum:~-%K%!!S!
  33. set /a sum=!sum:~0,1!-2
  34. )
  35. REM 显示
  36. :DISP
  37. for /l %%i in (1 1 8) do (if "!S:~0,1!"=="0" (set S=!S:~1!))
  38. if "!S!"=="" (set S=0)
  39. echo,!S!
  40. endlocal
  41. goto :eof
  42. REM 分割数位
  43. :POINT
  44. set num=%1
  45. for /l %%i in (1 1 !CYC!) do (
  46. set %2[%%i]=!num:~-%K%!
  47. set num=!num:~0,-%K%!
  48. if "!num!"=="" (
  49. set /a CU=!%2[%%i]!+100000000
  50. set %2[%%i]=!CU:~-%K%!
  51. set /a %3=%%i
  52. goto :eof
  53. )
  54. )
复制代码
Sublime_text 显示的运行时间 [Finished in 0.6s]

hongrk 的测试代码
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set num_a=456456521000210000000000000000874112115674511111111111111110019999999
  4. set num_b=923451344221111111111111111000000000001
  5. for /l %%a in (1,1,6) do set num_a=!num_a!!num_a!
  6. for /l %%a in (1,1,6) do set num_b=!num_b!!num_b!
  7. call :jia %num_a% %num_b% P
  8. echo %P%
  9. exit
  10. :jia
  11. setlocal enabledelayedexpansion&set c1=%~1&set c2=%~2&set c=0&set t=
  12. :j
  13.     if %c%==0 (
  14.         if "%c1%"=="" endlocal &set %~3=%c2%%t%&goto :eof
  15.         if "%c2%"=="" endlocal &set %~3=%c1%%t%&goto :eof
  16.     )
  17.     ::此句为“加速版”增添的代码。
  18.     set c1=000000000%c1%
  19.     set c2=000000000%c2%
  20.     set /a t=1!c1:~-9!-1000000000+1!c2:~-9!-1000000000+c,c=0&set c1=!c1:~9,-9!&set c2=!c2:~9,-9!&if "!c1!!c2!"=="" (set t=!t!%t%) else (if not "!t:~9!"=="" set c=1)&set t=00000000!t!&set t=!t:~-9!%t%&goto j
  21.     endlocal&set %~3=%t%&goto :eof
复制代码
Sublime_text 显示的运行时间 [Finished in 0.6s]

TOP

本帖最后由 523066680 于 2019-4-12 21:54 编辑

循着 Modern Computer Arithmetic 的方案:

β 就是 Base,对于 int32 尺寸的容器可以选 10^9 作为 Base。

也写了一个for版本,其实和已经出现的代码都是差不多的:
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. ::constant
  4. set /a BASE=1000000000, LEN=9
  5. set MASK=!BASE:~1!
  6. ::test
  7. set num_a=456456521000210000000000000000874112115674511111111111111110019999999
  8. set num_b=923451344221111111111111111000000000001
  9. for /l %%a in (1,1,6) do set num_a=!num_a!!num_a!
  10. for /l %%b in (1,1,6) do set num_b=!num_b!!num_b!
  11. call :plus %num_a% %num_b% sum
  12. echo %sum%
  13. exit /b
  14. :plus
  15.     setlocal enabledelayedexpansion
  16.     set "sum=" & set "va=%1" & set "vb=%2"
  17.     set /a carry=0
  18.     :: 1 to 1000, because max strlen < 8192
  19.     for /l %%a in (1,1,1000) do (
  20.         set /a a=1!va:~-%LEN%, %LEN%!-BASE, b=1!vb:~-%LEN%,%LEN%!-BASE
  21.         set /a t=a+b+carry, carry=t/BASE, head=t
  22.         set t=!MASK!!t!
  23.         set va=!MASK!!va:~0,-%LEN%!
  24.         set vb=!MASK!!vb:~0,-%LEN%!
  25.         if "!va:0=!!vb:0=!" == "" (goto :next)
  26.         set sum=!t:~-%LEN%!!sum!
  27.     )
  28.     :next
  29.     endlocal&set %3=%head%%sum%&goto :eof
复制代码
Sublime_Text 显示耗时:[Finished in 0.5s]

各位大神拍砖轻点,批处理这门语言我很久都没学会,经常写出很长很臃肿的代码,所以早就换成其他语言了,语法糖很甜的那种。

补充:
    并不香,写了我也用不着。
以前的那些帖子,过去式,人家也没有那么刻意地去追求速度,实现就足够了。特别地,批处理只是批处理,有自己的定位,
一个连运算符重载都没有的脚本语言,搞四则运算,又要当坠吼的那个 —— 写到 karatsuba 乘法方案的时候你就知道烦了。
4

评分人数

TOP

早就脱坑,对我个人而言,没有任何理由再讨论bat。(一句话:我走错片场了
523066680 发表于 2019-4-6 17:41



    真香现场

TOP

回复 17# 523066680


    出门前看到你的回帖,很想立刻回复并改进代码,但时间不够。现在补上。

Bat在效率上天生就不敌很多语言,但毕竟还有能力尽可能追求完美,当然还是想尽量写得好一些。
实用性来讲的话,我目前帖子里的加法函数和除法函数我自己就在用,正好满足需求。不过更多算是练习吧,毕竟Bat算再慢也已经比输入快了啊。

从你的代码里面意识到自己代码里还有一些可以优化的地方,比如进位符的处理,用除法比置0再检测要简洁很多。
还有一个就是那个for指令;我曾也想过用for,因为goto效率确实低些,但for循环不管怎样都要运行完 的特性实在是太不人性了。比如说算一个1+1和算几百位数加减,时间只有1倍左右的差距。happy的代码里用的是先测长度,再根据长度定循环次数,但我觉得那样的话测长度就得费很多时间了,最后还是用了最原始的goto循环。

发现call+goto可以成功跳出for循环。
代码已改进。

TOP

本帖最后由 523066680 于 2019-4-13 12:24 编辑

No.1,你的第三版代码和第二版结果不一致,相同参数

TOP

回复 20# 523066680


    我检查了一遍,没发现有错啊。是不是加数有变,我记不清了。

TOP

本帖最后由 523066680 于 2019-4-13 16:13 编辑
  1. use List::MoreUtils qw/pairwise mesh/;
  2. my ($f1, $f2) = ("hongrk2.bat", "hongrk3.bat");
  3. my @s1 = `$f1`;
  4. my @s2 = `$f2`;
  5. my @foo = unpack("(A40)*", $s1[1]);
  6. my @bar = unpack("(A40)*", $s2[1]);
  7. printf "%40s   %40s\n", $f1, $f2;
  8. pairwise { printf "%s %s %s\n", $a, $a eq $b?" ":"!", $b } @foo, @bar;
复制代码
  1.                              hongrk2.bat                                hongrk3.bat
  2. 4564565210002100000000000000008741121156   4564565210002100000000000000008741121156
  3. 7451111111111111111001999999945645652100   7451111111111111111001999999945645652100
  4. 0210000000000000000874112115674511111111   0210000000000000000874112115674511111111
  5. 1111111100199999994564565210002100000000   1111111100199999994564565210002100000000
  6. 0000000087411211567451111111111111111001   0000000087411211567451111111111111111001
  7. 9999999456456521000210000000000000000874   9999999456456521000210000000000000000874
  8. 1121156745111111111111111100199999994564   1121156745111111111111111100199999994564
  9. 5652100021000000000000000087411211567451   5652100021000000000000000087411211567451
  10. 1111111111111110019999999456456521000210   1111111111111110019999999456456521000210
  11. 0000000000000008741121156745111111111111   0000000000000008741121156745111111111111
  12. 1111001999999945645652100021000000000000   1111001999999945645652100021000000000000
  13. 0000874112115674511111111111111110019999   0000874112115674511111111111111110019999
  14. 9994564565210002100000000000000008741121   9994564565210002100000000000000008741121
  15. 1567451111111111111111001999999945645652   1567451111111111111111001999999945645652
  16. 1000210000000000000000874112115674511111   1000210000000000000000874112115674511111
  17. 1111111111100199999994564565210002100000   1111111111100199999994564565210002100000
  18. 0000000000087411211567451111111111111111   0000000000087411211567451111111111111111
  19. 0019999999456456521000210000000000000000   0019999999456456521000210000000000000000
  20. 8741121156745111111111111111100199999994   8741121156745111111111111111100199999994
  21. 5645652100021000000000000000087411211567   5645652100021000000000000000087411211567
  22. 4511111111111111110019999999456456521000   4511111111111111110019999999456456521000
  23. 2100000000000000008741121156745111111111   2100000000000000008741121156745111111111
  24. 1111111001999999945645652100021000000000   1111111001999999945645652100021000000000
  25. 0000000874112115674511111111111111110019   0000000874112115674511111111111111110019
  26. 9999994564565210002100000000000000008741   9999994564565210002100000000000000008741
  27. 1211567451111111111111111001999999945645   1211567451111111111111111001999999945645
  28. 6521000210000000000000000874112115674511   6521000210000000000000000874112115674511
  29. 1111111111111100199999994564565210002100   1111111111111100199999994564565210002100
  30. 0000000000000087411211567451111111111111   0000000000000087411211567451111111111111
  31. 1110019999999456456521000210000000000000   1110019999999456456521000210000000000000
  32. 0008741121156745111111111111111100199999   0008741121156745111111111111111100199999
  33. 9945645652100021000000000000000087411211   9945645652100021000000000000000087411211
  34. 5674511111111111111110019999999456456521   5674511111111111111110019999999456456521
  35. 0002100000000000000008741121156745111111   0002100000000000000008741121156745111111
  36. 1111111111001999999945645652100021000000   1111111111001999999945645652100021000000
  37. 0000000000874112115674511111111111111110   0000000000874112115674511111111111111110
  38. 0199999994564565210002100000000000000008   0199999994564565210002100000000000000008
  39. 7411211567451111111111111111001999999945   7411211567451111111111111111001999999945
  40. 6456521000210000000000000000874112115674   6456521000210000000000000000874112115674
  41. 5111111111111111100199999994564565210002   5111111111111111100199999994564565210002
  42. 1000000000000000087411211567451111111111   1000000000000000087411211567451111111111
  43. 1111110019999999456456521000210000000000   1111110019999999456456521000210000000000
  44. 0000008741121156745111111111111111100199   0000008741121156745111111111111111100199
  45. 9999945645652100021000000000000000087411   9999945645652100021000000000000000087411
  46. 2115674511111111111111110019999999456456   2115674511111111111111110019999999456456
  47. 5210002100000000000000008741121156745111   5210002100000000000000008741121156745111
  48. 1111111111111001999999945645652100021000   1111111111111001999999945645652100021000
  49. 0000000000000874112115674511111111111112   0000000000000874112115674511111111111112
  50. 0334713442205675676321113210000000000019   0334713442205675676321113210000000000019
  51. 2432545633678562222222222211111002000192 ! 2432545633678562222222222211111001900192
  52. 2907800742111321111111111000000874114039 ! 2907800742111321111111111000000874114038
  53. 1258553322222222222211309999994564584444 ! 1258553322222222222211309999994564574444
  54. 5155422111111111111198511211567451303456   5155422111111111111198511211567451303456
  55. 2455332221131111110567456521000211923451   2455332221131111110567456521000211923451
  56. 3442211119852232267855111111111130345613 ! 3442211119852232267855111111111120345613
  57. 6422111056756763211121000000000192345221 ! 6322111056756763211121000000000192345121
  58. 8333226785622222222111111110021923450800 ! 8333226785622222222111111110021922450800
  59. 6776321113211111110000000008760355670187 ! 6776321113211111110000000008750355670187
  60. 3222222222222222101999999945837997234443   3222222222222222101999999945837997234443
  61. 1111111111111111874112115676434562455332   1111111111111111874112115676434562455332
  62. 2222211311111104564565210021334513442211 ! 2222211301111104564565210011334513442211
  63. 1111198522322667451111111303456245424111   1111198522322667451111111303456245424111
  64. 1110567567632000210000001923451345095223 ! 0110567567632000210000001923451345095223
  65. 2267856222221111111111119434513436775676   2267856222221111111111119434513436775676
  66. 3211132111100000000000279756345989562222   3211132111100000000000279756345989562222
  67. 2222222222110020000001379907865221321111 ! 2222222222110019000001379907865221321111
  68. 1111111110008741121175979624553322222222 ! 1111111110008741121165979624553322222222
  69. 2113111099945645652292366134422111111111   2113111099945645652292366134422111111111
  70. 1985223115674511113034562455331131111110   1985223115674511113034562455331131111110
  71. 5675675210002100019234513442219852232267   5675675210002100019234513442219852232267
  72. 8562211111111111303347134422056756763211 ! 8562211111111111303347034422056756763211
  73. 1321000000000001924325456336785622222222 ! 1321000000000001923325456336785622222222
  74. 2221111100200019229078007421113211111111 ! 2221111100200009229078007421113211111111
  75. 1100000087411403912585533222222222222113   1100000087411403912585533222222222222113
  76. 0999999456458444451554221111111111111985   0999999456458444451554221111111111111985
  77. 1121156745130345624553322211311111105674 ! 1121156745120345624553322211301111105674
  78. 5652100021192345134422111198522322678551   5652100021192345134422111198522322678551
  79. 1111111113034561364221110567567632111210 ! 1111111113034561364220110567567632111210
  80. 0000000019234522183332267856222222221111   0000000019234522183332267856222222221111
  81. 1111002192345080067763211132111111100000   1111002192345080067763211132111111100000
  82. 0000876035567018732222222222222221019999   0000876035567018732222222222222221019999
  83. 9994583799723444311111111111111118741121 ! 9994573799723444311111111111111118741121
  84. 1567643456245533222222113111111045645652 ! 1567643456245533222222113111011045645652
  85. 1002133451344221111111985223226674511111   1002133451344221111111985223226674511111
  86. 1130345624542411111105675676320002100000 ! 1120345624542411111105675676320002100000
  87. 0192345134509522322678562222211111111111   0192345134509522322678562222211111111111
  88. 1943451343677567632111321111000000000002 ! 1942451343677567632111321111000000000001
  89. 7975634598956222222222222221100200000013 ! 7975634598956222222222222221100199990013
  90. 7990786522132111111111111100087411211759   7990786522132111111111111100087411211759
  91. 7962455332222222221131110999456456522923   7962455332222222221131110999456456522923
  92. 6613442211111111119852231156745111130345   6613442211111111119852231156745111130345
  93. 6245533113111111056756752100021000192345 ! 6245533113011111056756752100021000192345
  94. 1344221985223226785622111111111113033471 ! 1344221985223226785622111111111112033471
  95. 3442205675676321113210000000000019243254 ! 3432205675676321113210000000000019243254
  96. 5633678562222222222211111002000192290780   5633678562222222222211111002000192290780
  97. 0742111321111111111000000874114039125855 ! 0742111321111111111000000874113039125855
  98. 3322222222222211309999994564584444515542   3322222222222211309999994564584444515542
  99. 2111111111111198511211567451303456245533   2111111111111198511211567451303456245533
  100. 2221131111110567456521000211923451344221 ! 2221131110110567456521000211923451344221
  101. 1119852232267855111111111130345613642211   1119852232267855111111111130345613642211
  102. 1056756763211121000000000192345221833322   1056756763211121000000000192345221833322
  103. 6785622222222111111110021923450800677632 ! 6785622222222111111110020923450800677632
  104. 1113211111110000000008760355670187322222 ! 1113211111110000000008760355660187322222
  105. 2222222222101999999945837997234443111111   2222222222101999999945837997234443111111
  106. 1111111111874112115676434562455332222221 ! 1111111111874112115675434562455332222221
  107. 1311111104564565210021334513442211111119   1311111104564565210021334513442211111119
  108. 8522322667451111111303456245424111111056 ! 8522322667451111111303456245424011111056
  109. 7567632000210000001923451345095223226785 ! 7567632000210000001923451344095223226785
  110. 6222221111111111119434513436775676321113 ! 6222221111111111119434503436775676321113
  111. 2111100000000000279756345989562222222222   2111100000000000279756345989562222222222
  112. 2222110020000000   2222110020000000
复制代码

TOP

本帖最后由 523066680 于 2019-4-13 21:48 编辑

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

TOP

回复 22# 523066680


    第二版试了好几次,都是提示过长闪退,不知道具体结果多少。不过在你给出的2个结果中,第三版的那个是对的,是第二版错了。

自然,其他语言快得多也方便得多;但目前我还只想先练好Bat。发出来主要还是想让别人指出问题方便改进,而这点基本达到了。肯定迟早会去试着用其他语言,但顺其自然吧。至少先把手头的做掉

TOP

回复 22# 523066680


    后来更新的时候发现确实是有问题,很抱歉。不过已经处理好了。
二分法数位数的效率极佳,而goto循环的效率极差,二者相较下,采取数位以便改用for循环才是最合适的,我之前的想法有些偏颇。
改写后的代码计算之前那个例子,1次所需时间在110-120ms左右,有四五倍的提升。
1

评分人数

TOP

返回列表