计算程序运行时间:console.time

计算程序运行时间:console.time,第1张

记录一个 *** 作的占用时长

启动计时器(timer)来跟踪某个 *** 作的占用时长,每个计时器必须拥有唯一的名字,当此计时器名字为参数调用 consoletimeEnd() 时,浏览器将以毫秒为单位,输出对应的计时器所经过的时间

timerName :新计时器的名字。用于标记这个计时器,做为参数调用 consoletimeEnd() 可以停止计时并将经过的时间在终端中打印出来

MDN:consoletime

将scanf输入换成从文件读取数据,使用fopen, fread, fwrite之类的函数,不要从终端上直接输入。或者使用重定向<从文件读取数据。

如果效率还是不行,再将printf改成输出到文件中。

注意函数的优化比如if(){}if(){}这样的函数之间应该加上return;if(){return;}if(){return;}这样就减少判断次数

#include <stdioh>

#include <mathh>

void main()

{

int t,i,j,s;

int a;

double q;

scanf("%d",&t);

for(i=1;i<=t;i++)

{

s=1;

scanf("%d",&a);

for(j=2,q=sqrt(a);j<q;j++)

{

if(a%j==0)

{

s += j;

s += a/j;

}

}

j = (int)q;//判断乘方数

if(a==jj) s+=j;

printf("%d\n",s);

}

}

namespace BIT {

template<typename T>

class BIT {

BIT(BIT&); //no implementation

T _a;

T _c;

int _len;

void clean();

T calcC(int i);

public:

BIT();

~BIT();

void LoadArray(T arr, int len);

void Update(int nIndex, T val);

T GetSum(int cnt);

private:

static int removeLastBit(int n);

static int lastBitPos(int n);

};

template<typename T>

BIT<T>::BIT()

{

_c = 0;

_a = 0;

}

template<typename T>

BIT<T>::~BIT()

{

clean();

}

template<typename T>

void BIT<T>::clean()

{

if (_c != 0) delete[] _c;

_c = 0;

_a = 0;

}

template<typename T>

void BIT<T>::LoadArray(T arr, int len)

{

clean();

_len = len;

_a = arr;

_c = new T[_len];

_c[0] = _a[0];

for (int i = 0; i < len; ++i)

_c[i] = calcC(i);

}

template<typename T>

T BIT<T>::calcC(int i)

{

int pos;

//get the pos of last 1

if (i == 0)

pos = 0;

else

pos = lastBitPos(i);

if (pos == 0) return _a[i];

T r = _a[i];

int startSubIndex = 1;

for(; pos > 0; --pos) {

r += _c[i - startSubIndex];

startSubIndex <<= 1;

}

return r;

}

template<typename T>

T BIT<T>::GetSum(int cnt)

{

if (cnt < 0) return 0;

T r = _c[cnt];

while(cnt != 0) {

cnt = removeLastBit(cnt);

r += _c[cnt];

}

return r;

}

template<typename T>

void BIT<T>::Update(int nIndex, T val)

{

_a[nIndex] = val;

_c[nIndex] = calcC(nIndex);

int toAdd = 1;

for(;;) {

nIndex += toAdd;

if (nIndex > _len) break;

_c[nIndex] = calcC(nIndex);

toAdd <<= 1;

}

}

template<typename T>

int BIT<T>::removeLastBit(int n)

{

int t = n & (-n);

n ^= t;

return n;

}

template<typename T>

int BIT<T>::lastBitPos(int n)

{

int t = n & (-n);

int r = 0;

if (t & 0xffff0000) {

r += 16;

t >>= 16;

}

if (t & 0xff00) {

r += 8;

t >>= 8;

}

if (t & 0xf0) {

r += 4;

t >>= 4;

}

if (t & 0xc) {

r += 2;

t >>= 2;

}

if (t & 2) {

r += 1;

t >>= 1;

}

return r;

}

};

#include <iostream>

using namespace std;

int main()

{

int len, cnt;

cin >> len >> cnt;

int datas = new int[len];

int i;

for(i = 0; i < len; ++i)

cin >> datas[i];

BIT::BIT<int> t;

tLoadArray(datas, len);

for(i = 0; i < cnt; ++i) {

int fr, to;

cin >> fr >> to;

if (fr == to)

cout << datas[fr - 1] << endl;

else

cout << tGetSum(to - 1) - tGetSum(fr - 2) << endl;

}

return 0;

}

这题要用树状数组去做,树状数组求前缀和的时间复杂度是O(log n)

你这样死算,复杂度O(n),来不及的。

手头刚好有写好的代码,就套进来用用看,不确定有没有bug,你的测试数据能过就是了……

关于这个数据结构的详细,网上搜索“树状数组”,花点时间学一下

以上就是关于计算程序运行时间:console.time全部的内容,包括:计算程序运行时间:console.time、c语言程序,时间超限,怎么解决、如何缩短c语言程序的运行时间等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/10170895.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-06
下一篇2023-05-06

发表评论

登录后才能评论

评论列表(0条)

    保存