Bu çok eski iplik olduğunu biliyorum, ama ben boost olmadan aynı çözüm ve en hızlı biri için isteyen herkese bu eklemek istiyorum.
ben yukarıdaki üç farklı öneri ve/dev/null doğrudan bir yazma (o çekirdek içerir böylece.)
Şaşırtıcı en fazla oyu NullStream en kötü performansı birleştirdi.Biz badbit kullanmak - - bir dezavantajı vardır
İşte
a) /dev/null : 30 seconds
b) NullStream: 50 seconds
c) badbit : 16 seconds (the winner in speed, but cannot test for errors!)
d) boost : 25 seconds (the ultimate winner)
test kodu
#include <iostream>
#include <fstream>
#include <time.h>
#include <boost/iostreams/stream.hpp>
class NullStream : public std::ostream {
class NullBuffer : public std::streambuf {
public:
int overflow(int c) { return c; }
} m_nb;
public:
NullStream() : std::ostream(&m_nb) {}
};
int test(std::ostream& ofs, const char* who) {
const time_t t = time(NULL);
for (int i = 0 ; i < 1000000000 ; i++)
ofs << "Say the same" ;
std::cout << who << ": " << time(NULL) - t << std::endl;
}
void devnull() {
std::ofstream ofs;
ofs.open("/dev/null", std::ofstream::out | std::ofstream::app);
test(ofs, __FUNCTION__);
ofs.close();
}
void nullstream() {
NullStream ofs;
test(ofs, __FUNCTION__);
}
void badbit() {
std::ofstream ofs;
ofs.setstate(std::ios_base::badbit);
test(ofs, __FUNCTION__);
}
void boostnull() {
boost::iostreams::stream<boost::iostreams::null_sink> nullOstream((boost::iostreams::null_sink()));
test(nullOstream, __FUNCTION__);
}
int main() {
devnull();
nullstream();
badbit();
boostnull();
return 0;
}
DÜZENLEME
en hızlı çözümdür:
İşte 100,000,000 yazma için sonuçlardır. Program, çıktıyı başarılı bir şekilde yazıp yazmadığını kontrol ederse ve programın neden bunu yapmaması gerektiğine dair bir fikrim yok - o zaman bu hatadan dolayı başarısız olacaktır. Bu nedenle, kazanan - bost - kazanan.
'/ dev/null' öğesini' std :: ofdream'de açabilirsiniz –
Taşınabilir bir şeye ihtiyacınız var, @Alexandre – paperjam
Anladım (aksi halde bu bir cevap olurdu) http://stackoverflow.com/questions/ Ancak, 313111/dev-null-in-windows yeterli durumu kapsamalıdır. –