缓慢的setfill和setw函数

最近在做数据结构的题,https://pta.patest.cn/pta/test/1342/exam/4/question/19210 这个题怎么写都超时,然后就从各个角度抠时间。

原因找遍,估计是setfill和setw函数占用时间比较多,改为printf后立马就通过了。

例如:

        cout<<setfill('0')<<setw(5);
        cout<<A[i].Addr;
        cout<<setw(0);
        cout<<' '<<A[i].Data<<' ';
        if (A[i].NextAddr!=-1) cout<<setfill('0')<<setw(5);
        cout<<A[i].NextAddr<<endl;

(这两个函数还需要#include <iomanip>的支持)
改为

printf("%05d %d %05d\n",B[i].Addr,B[i].Data,B[i].NextAddr);

耗时大大缩短。

想用VS的性能向导测耗时,但我的VS2010出bug用不了,新版的要WIN10系统,懒得换了。

猜你喜欢

转载自blog.csdn.net/tomwillow/article/details/52613903