Stamon 文档站

回到首页
回到上一级

2025/10/05 工作日志

本次更新的内部版本为2.4.53. 本次更新不会发布新版本。 本次更新以重构底层库为首要任务,对整个项目进行了大规模整改。

重新布局移植接口

从今往后,“依赖库”这一称谓将会升级为“移植接口”。

我将include/template目录重命名为更方便理解的include/interface目录。 为了进行更有效且通用的接口约束,我参考了CRTPconcept和纯虚函数设计,自创了CITP这一设计。CITP初步原理的讲解位于CITP设计文档.md,CITP是个方便、易懂且几乎零成本抽象的设计,应该被广泛引用。

重构后的移植接口设计总述见移植接口开发指导.md。我们来看一看这一次重构对于接口的更改有哪些:

接下来,我可能会引入移植接口的STL实现。

修改了大量细节

优化程序性能

在重新选用Map的数据结构与算法,加之大量细节处修改之后,Stamon的性能得到了飞跃性提升。

为了更直观、客观的看到优化前后的差距,我选取了2.4.49发行版与2.4.53内部版本进行对比,两个版本都使用Makefile编译。测试在VirtualBox中的Alpine Linux v3.22 x86_64运行,我采用perf 6.15.0作为性能测试工具。

我们测试了三个样例:十万次空循环、朴素递归计算斐波那契数列第三十项、从2到50000的埃氏素数筛。

十万次空循环的性能测试

生成的原始结果分别如下:

# 优化前
# started on Sun Oct  5 18:25:30 2025


 Performance counter stats for 'stamon_old run demos/loop_old.stvc':

           5996.75 msec task-clock                       #    0.997 CPUs utilized             
               159      context-switches                 #   26.514 /sec                      
                 0      cpu-migrations                   #    0.000 /sec                      
              8965      page-faults                      #    1.495 K/sec                     
   <not supported>      cycles                                                                

       6.011989689 seconds time elapsed

       5.617046000 seconds user
       0.380536000 seconds sys
# 优化后
# started on Sun Oct  5 18:23:02 2025


 Performance counter stats for 'stamon run demos/loop_new.stvc':

            232.40 msec task-clock                       #    0.974 CPUs utilized             
                 9      context-switches                 #   38.726 /sec                      
                 0      cpu-migrations                   #    0.000 /sec                      
              3639      page-faults                      #   15.658 K/sec                     
   <not supported>      cycles                                                                

       0.238499505 seconds time elapsed

       0.066635000 seconds user
       0.166589000 seconds sys
朴素递归计算斐波那契数列第三十项的性能测试

生成的原始结果分别如下:

# 优化前
# started on Sun Oct  5 17:58:51 2025


 Performance counter stats for 'stamon_old run demos/fib_old.stvc':

         230904.82 msec task-clock                       #    1.000 CPUs utilized             
                10      context-switches                 #    0.043 /sec                      
                 0      cpu-migrations                   #    0.000 /sec                      
             39240      page-faults                      #  169.940 /sec                      
   <not supported>      cycles                                                                

     230.934695647 seconds time elapsed

      72.922803000 seconds user
     157.999407000 seconds sys
# 优化后
# started on Sun Oct  5 18:15:50 2025


 Performance counter stats for 'stamon run demos/fib_new.stvc':

          11680.72 msec task-clock                       #    0.998 CPUs utilized             
                40      context-switches                 #    3.424 /sec                      
                 0      cpu-migrations                   #    0.000 /sec                      
             26722      page-faults                      #    2.288 K/sec                     
   <not supported>      cycles                                                                

      11.705414774 seconds time elapsed

       4.247985000 seconds user
       7.433974000 seconds sys
从2到50000的埃氏素数筛的性能测试

生成的原始结果分别如下:

# 优化前
# started on Sun Oct  5 18:28:54 2025


 Performance counter stats for 'stamon_old run demos/nsieve_old.stvc':

          19057.96 msec task-clock                       #    0.998 CPUs utilized             
                26      context-switches                 #    1.364 /sec                      
                 1      cpu-migrations                   #    0.052 /sec                      
             19145      page-faults                      #    1.005 K/sec                     
   <not supported>      cycles                                                                

      19.087737930 seconds time elapsed

      12.488769000 seconds user
       6.573036000 seconds sys
# 优化后
# started on Sun Oct  5 18:27:53 2025


 Performance counter stats for 'stamon run demos/nsieve_new.stvc':

            796.67 msec task-clock                       #    0.988 CPUs utilized             
                10      context-switches                 #   12.552 /sec                      
                 0      cpu-migrations                   #    0.000 /sec                      
              8652      page-faults                      #   10.860 K/sec                     
   <not supported>      cycles                                                                

       0.806208362 seconds time elapsed

       0.114124000 seconds user
       0.684745000 seconds sys
对优化前后的测试分析和结论

上述原始测试结果被数据提取并整理成了以下表格:

测试项 优化前耗时(按秒计) 优化后耗时(按秒计)
十万次空循环 6.011989689 0.238499505
朴素递归计算斐波那契数列第三十项 230.934695647 11.705414774
从2到50000的埃氏素数筛 19.087737930 0.806208362

不难看出,得益于哈希表的优秀性能和细节处的代码优化,Stamon的性能得到了质的飞跃。


查看该文件的提交记录