मैं मानक आउटपुट में पाठ मुद्रित करने के विभिन्न तरीकों के बीच अंतर का समय लगा रहा हूं। मैं cout
, printf
, और ostringstream
दोनों \n
और std::endl
का उपयोग कर परीक्षण कर रहा हूं। (और ऐसा किया) के साथ अंतर बनाने के लिए मुझे std::endl
की उम्मीद थी (और ऐसा हुआ), लेकिन मुझे उम्मीद नहीं थी कि यह ostringstream
के साथ आउटपुट धीमा कर देगा। मैंने सोचा कि std::endl
का उपयोग केवल स्ट्रीम में \n
लिख देगा और यह अभी भी एक बार फिर फ्लश हो जाएगा। यहाँ क्या चल रहा है?ostringstream के साथ std :: endl का उपयोग आउटपुट गति को प्रभावित क्यों करता है?
// cout.cpp
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 10000000; i++) {
cout << "Hello World!\n";
}
return 0;
}
// printf.cpp
#include <stdio.h>
int main() {
for (int i = 0; i < 10000000; i++) {
printf("Hello World!\n");
}
return 0;
}
// stream.cpp
#include <iostream>
#include <sstream>
using namespace std;
int main() {
ostringstream ss;
for (int i = 0; i < 10000000; i++) {
ss << "stream" << endl;
}
cout << ss.str();
}
// streamn.cpp
#include <iostream>
#include <sstream>
using namespace std;
int main() {
ostringstream ss;
for (int i = 0; i < 10000000; i++) {
ss << "stream\n";
}
cout << ss.str();
}
और यहाँ मेरी Makefile
SHELL:=/bin/bash
all: cout.cpp printf.cpp
g++ cout.cpp -o cout.out
g++ printf.cpp -o printf.out
g++ stream.cpp -o stream.out
g++ streamn.cpp -o streamn.out
time:
time ./cout.out > output.txt
time ./printf.out > output.txt
time ./stream.out > output.txt
time ./streamn.out > output.txt
है यहाँ मैं क्या मिलता है जब मैं make
पीछा चलाने से make time
time ./cout.out > output.txt
real 0m1.771s
user 0m0.616s
sys 0m0.148s
time ./printf.out > output.txt
real 0m2.411s
user 0m0.392s
sys 0m0.172s
time ./stream.out > output.txt
real 0m2.048s
user 0m0.632s
sys 0m0.220s
time ./streamn.out > output.txt
real 0m1.742s
user 0m0.404s
sys 0m0.200s
इन परिणामों संगत कर रहे हैं: यहाँ अपने सभी कोड है।
यह शायद आपके प्रश्न का उत्तर देगा: http://stackoverflow.com/questions/213907/c-stdendl-vs-n संक्षेप में: हाँ, std :: endl आउटपुट – stefan
को फ्लश करता है मुझे नहीं पता कि मैंने पढ़ा है या नहीं यह सवाल गलत है, लेकिन मुझे लगता है कि वह पूछ रहा था कि स्ट्रिंगस्ट्रीम को फ्लश करने का प्रदर्शन मुद्दा क्यों होगा। स्ट्रिंगस्ट्रीम को फ्लश करने के लिए अतिरिक्त काम क्या करता है जिसे कॉल को '.str() 'पर नहीं छोड़ा जा सकता है? 'Cout' के लिए, टर्मिनल को प्रतिपादन में समय लगेगा, क्या स्ट्रिंगस्ट्रीम को फ़्लश करने के कोई दृश्य दुष्प्रभाव हैं? – loganfsmyth
@stefan मैंने वास्तव में उस प्रश्न को पढ़ लिया है, लेकिन कुछ हिस्सों में मैं सोच रहा हूं कि स्ट्रिंगस्ट्रीम को फ़्लश करने का क्या अर्थ है। मैंने सोचा कि यह अनिवार्य रूप से कुछ भी नहीं करेगा, लेकिन स्पष्ट रूप से मैं गलत हूँ। – gsingh2011