Constant in Printf
-
Ich möchte mit printf() einen string und einen Const. ausgeben jedoch klappt das nicht:
const std::string CONSTANT = "CONSTANT"; printf("String", CONSTANT);
-
Warum nutzt du printf in C++? Wenn du es schon nutzt, dann musst du ganz genau wissen, was du tust, weil du dann C und C++ mischt. Anscheinend ist dir aber nicht einmal ansatzweise klar, wie printf funktioniert. Was mich wieder zurück zu der Frage bringt: Warum printf?
-
Mir ist aufgefallen das "cout" langsamer ist wie printf weshalb ich nun mit print arbeiten möchte
-
printf ist C und wenn du einen String ausgeben willst, dann setze %s zwischen die '"' ist aber schwachsinn wenn du in C++ programmierst
-
Siehe hier:
std::cout test: 1.125 s ; printf test: 0.195 s
std::cout test: 1.154 s ; printf test: 0.230 s
std::cout test: 1.142 s ; printf test: 0.216 s
std::cout test: 1.322 s ; printf test: 0.221 s
std::cout test: 1.108 s ; printf test: 0.232 s
-
Sanjoy schrieb:
Mir ist aufgefallen das "cout" langsamer ist wie printf weshalb ich nun mit print arbeiten möchte
nicht überraschend, wenn printf sehr schnell gar nicht funktioniert...
-
Sanjoy schrieb:
Mir ist aufgefallen das "cout" langsamer ist wie printf weshalb ich nun mit print arbeiten möchte
Schreib Dir ein eigenes cout, das ist noch schneller.
Und zeig das Messprogramm.
-
int main(int argc, char* argv[]) { const char* teststring = "Test output string\n"; std::clock_t start; double duration; std::cout << "Starting std::cout test." << std::endl; start = std::clock(); for (int i = 0; i < 1000; i++) std::cout << teststring; /* ======================================================== */ for (int i = 0; i < 1000; i++) { std::printf(teststring); std::fflush(stdout); } /*==============================================================*/ return 0; }
-
Sanjoy schrieb:
Mir ist aufgefallen das "cout" langsamer ist wie printf weshalb ich nun mit print arbeiten möchte
Und aus welchem Grund denkst du, dass die Performance deiner Ausgabeoperationen einen wesentlichen Einfluss auf die Performance deines Programmes hat?
-
Sanjoy schrieb:
Ich möchte mit printf() einen string und einen Const. ausgeben jedoch klappt das nicht:
const std::string CONSTANT = "CONSTANT"; printf("String", CONSTANT);
Der schnellste Weg in die Hölle geht wohl so:
const std::string CONSTANT = "CONSTANT"; printf("String%s",*(char**)&CONSTANT);//gestestet, ist extrem schnell!!!
-
SeppJ schrieb:
Warum nutzt du printf in C++? Wenn du es schon nutzt, dann musst du ganz genau wissen, was du tust, weil du dann C und C++ mischt.
er mischt nicht C und C++, sondern er benutzt ein C++ Sprachfeature. Wenn ich in C++ #include benutze, mische ich dann C und C++?
-
Bei mir braucht printf 2.1 bis 2.4s und cout braucht 2.0 bis 2.4s
Je 10 Läufe, linux, gcc, urxvt, dein Messprog, Schleife bis 1Mio.
-
großbuchstaben schrieb:
SeppJ schrieb:
Warum nutzt du printf in C++? Wenn du es schon nutzt, dann musst du ganz genau wissen, was du tust, weil du dann C und C++ mischt.
er mischt nicht C und C++, sondern er benutzt ein C++ Sprachfeature. Wenn ich in C++ #include benutze, mische ich dann C und C++?
C und C++ Mischen ist in diesem Umfeld so definiert, dass man C Sprachfeatures verwendet, für die es in C++ eine bessere Alternative gibt.
-
Ok also sollte ich weiterhin cout verwenden. Sollte ich dann endl; oder "\n" verwenden? als Beispiel jetzt für einen Logger?
-
Für einen Logger...
Du könntest dir mal diese Libraries angucken - entweder zum Verwenden oder um abzugucken wie die schnell gemacht wurde, wenn sie es denn sind..
http://www.pantheios.org/
http://www.boost.org/doc/libs/1_57_0/libs/log/doc/html/index.html
http://fastformat.sourceforge.net/
http://johnwbyrd.github.io/logog/
http://sourceforge.net/projects/log4cplus/