c++ - When using ' ios::sync_with_stdio(0)' for testing difference in runtime, why is my output fragmented? -
clock_t tstart = clock(); ios::sync_with_stdio(0); cin.tie(0); for(int i=0;i<100000;++i) cout<<i; printf("\n\n\n\ntime taken: %.2fs\n", (double)(clock() - tstart)/clocks_per_sec); cout<<endl; using program cout runs in 4.05 seconds outputs numbers after executes final line. here's details:
time taken: 4.30s 99924999259992699927999289992999930999319993299933999349993599936999379993899939999409994199942999439994499945999469994799948999499995099951999529995399954999559995699957999589995999960999619996299963999649996599966999679996899969999709997199972999739997499975999769997799978999799998099981999829998399984999859998699987999889998999990999919999299993999949999599996999979999899999 using printf program runs intended slow (51 seconds). used endl after printing time flush output buffer. what's reason this?
well no longer synchronize c stdio (which printf uses) c++ stream i/o (which std::cout uses). means have different buffers may flushed (and written console) @ different times.
try adding std::cout << std::flush before printf call. or better yet, use std::cout last output well.
Comments
Post a Comment