codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
/********************************************** Comparison of timings for buffered output in D. */ enum nLines = 10_000_000; enum nTimes = 10; void main(string[] argv) { import std.stdio : writef; import std.datetime : benchmark; auto r = benchmark!(f0, f1, f2, f3, f4)(nTimes); foreach (i; 0 .. r.length) writef("fun[%d]: %6.1fs\n", i, r[i].msecs/(1000. * nTimes)); } void f0() { import std.stdio : File, writef; auto tmp = File("/tmp/_f0_output", "w"); foreach(i; 0 .. nLines) { tmp.writef("%d %d %0.8f\n", i, i*13, i/1e7); } } void f1() { import std.stdio : File; import std.array : appender; import std.format : formattedWrite; auto tmp = File("/tmp/_f1_output", "w"); auto app = appender!(char[]); app.reserve(1024 * 1024); foreach(i; 0 .. nLines) { if (app.capacity < 1024) { tmp.rawWrite(app.data); app.clear(); } app.formattedWrite("%d %d %0.8f\n", i, i*13, i/1e7); } if (app.data.length > 0) tmp.rawWrite(app.data); } void f2() { import std.stdio : File; import std.outbuffer; import std.format : formattedWrite; import std.file : write; auto tmp = File("/tmp/_f2_output", "w"); auto buf = new OutBuffer(); buf.reserve(nLines * 30); foreach(i; 0 .. nLines) { buf.formattedWrite("%d %d %0.8f\n", i, i*13, i/1e7); } tmp.write(buf.toString()); } void f3() { import core.stdc.stdio; auto tmp = fopen("/tmp/_f3_output", "w"); foreach(i; 0 .. nLines) { tmp.fprintf("%d %d %0.8f\n\0", i, i*13, i/1e7); } } void f4() { import core.stdc.stdio; char[30] tmp; foreach(i; 0 .. nLines) { sprintf(tmp.ptr, "%d %d %0.8f\n\0", i, i*13, i/1e7); } }
Private
[
?
]
Run code
Submit