当我在timeit()之外运行下面的代码时,它似乎立即完成.但是当我在timeit()函数中运行它时,需要更长的时间.为什么?
>>> import timeit
>>> t = timeit.Timer("3**4**5")
>>> t.timeit()
16.55522028637718
Run Code Online (Sandbox Code Playgroud)
使用:Python 3.1(x86) - AMD Athlon 64 X2 - WinXP(32位)
为什么在此代码完成时会获得额外的迭代(额外的行打印)?EOF需要额外的换行吗?我不想添加额外/特殊字符来标记EOF.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
ifstream infile("dictionary.txt"); // one word per line
string text;
while(infile){
infile >> text;
cout << text << endl;
}
infile.close();
return 0;
}
Run Code Online (Sandbox Code Playgroud)