我正在解析JSON以呈现报告。在解析字段rating
时,它可以有3个解析为JSONValue.integer
或3.4解析为JSONValue.floating
,我需要在报告中将它们转换为float。我已经分析了代码以寻找优化,并且将给定输入转换为浮点的get_number
函数花费了大量时间并且还使用了代码的异常。
我可以使用fast.d(https://github.com/mleise/fast),但我试图看看是否可以对占用时间的get_number函数进行更多优化。有没有办法在不使用异常的情况下获得浮点值,是否还有更多的优化可以完成?
redbus.d
import std.net.curl;
import std.exception;
import std.json;
import std.stdio;
import std.file;
import std.algorithm;
import std.array;
import std.conv;
import std.string;
auto fmt = "%35s | %10s | %10s | %10s | %15s";
auto value_fmt = "%35s | %10.2f | %10.0f | %10.2f | %15s";
void print_header() {
writeln("-----------------------------------------------------------------------------------------------");
writefln(fmt, "Name", "Price", "Window", "Rating", "Type");
writeln("-----------------------------------------------------------------------------------------------");
}
float get_number(T)(T item) {
float output = 0;
try {
output = to!float(item.integer);
} catch(Exception e) {
output = to!float(item.floating);
}
return to!float(output);
}
void print_item(T)(T item) {
writefln(value_fmt, item["Tvs"].str, item["MinFare"].get_number(), item["WnSt"].get_number(), item["Rtg"]["totRt"].get_number(), item["BusCategory"]["IsSleeper"]);
}
void main() {
auto content = readText("sample.json");
auto parsed = parseJSON(content);
auto raw = parsed["SRD"][0]["RIN"][0]["InvList"].array;
auto filtered = raw.filter!(item => to!bool(to!string(item["BusCategory"]["IsAc"])));
print_header();
foreach(item; filtered) {
print_item(item);
}
}
使用dmd -g -run redbus.d | grep redbus
======== Timer Is 3579545 Ticks/Sec, Times are in Microsecs ========
Num Tree Func Per
Calls Time Time Call
180 8673696 8300660 46114 @safe immutable(char)[] redbus.get_number!(std.json.JSONValue).get_number(std.json.JSONValue)
60 9448603 75423 1257 @safe void redbus.print_item!(std.json.JSONValue).print_item(std.json.JSONValue)
60 17581 10002 166 pure @safe void std.algorithm.iteration.__T12FilterResultS246redbus4mainFZ9__lambda1TAS3std4json9JSONValueZ.FilterResult.popFront()
2 110576 6987 3493 void redbus.print_header()
122 5635 5441 44 pure nothrow @property @nogc @safe bool std.algorithm.iteration.__T12FilterResultS246redbus4mainFZ9__lambda1TAS3std4json9JSONValueZ.FilterResult.empty()
60 4002 2877 47 pure nothrow ref @property @nogc @safe std.json.JSONValue std.algorithm.iteration.__T12FilterResultS246redbus4mainFZ9__lambda1TAS3std4json9JSONValueZ.FilterResult.front()
60 690 690 11 pure @safe bool redbus.main().__lambda1!(std.json.JSONValue).__lambda1(std.json.JSONValue)
2 386 348 174 pure ref @safe std.algorithm.iteration.__T12FilterResultS246redbus4mainFZ9__lambda1TAS3std4json9JSONValueZ.FilterResult std.algorithm.iteration.__T12FilterResultS246redbus4mainFZ9__lambda1TAS3std4json9JSONValueZ.FilterResult.__ctor(std.json.JSONValue[])
2 480 94 47 pure @safe std.algorithm.iteration.__T12FilterResultS246redbus4mainFZ9__lambda1TAS3std4json9JSONValueZ.FilterResult std.algorithm.iteration.__T6filterS246redbus4mainFZ9__lambda1Z.filter!(std.json.JSONValue[]).filter(std.json.JSONValue[])
2 28 28 14 pure nothrow @nogc @safe std.algorithm.iteration.__T12FilterResultS246redbus4mainFZ9__lambda1TAS3std4json9JSONValueZ.FilterResult std.algorithm.iteration.__T12FilterResultS246redbus4mainFZ9__lambda1TAS3std4json9JSONValueZ.FilterResult.opSlice()
使用dmd -g -run redbus.d
======== Timer Is 3579545 Ticks/Sec, Times are in Microsecs ========
Num Tree Func Per
Calls Time Time Call
180 8673696 8300660 46114 @safe immutable(char)[] redbus.get_number!(std.json.JSONValue).get_number(std.json.JSONValue)
378142 7487455 5101529 13 pure @safe dchar std.json.parseJSON!(immutable(char)[]).parseJSON(immutable(char)[], int, std.json.JSONOptions).popChar()
403760 11591911 4118861 10 pure @safe dchar std.json.parseJSON!(immutable(char)[]).parseJSON(immutable(char)[], int, std.json.JSONOptions).peekChar()
13218 11298746 3847457 291 @safe immutable(char)[] std.json.parseJSON!(immutable(char)[]).parseJSON(immutable(char)[], int, std.json.JSONOptions).parseString()
2 3416630 3410981 1705490 @safe void[] std.file.read!(immutable(char)[]).read(immutable(char)[], ulong)
11694 26577653 2975245 254 @safe void std.json.parseJSON!(immutable(char)[]).parseJSON(immutable(char)[], int, std.json.JSONOptions).parseValue(ref std.json.JSONValue)
2 3189533 2760109 1380054 pure @safe void std.utf.validate!(immutable(char)[]).validate(const(immutable(char)[]))
50558 9391716 2176452 43 pure @safe void std.json.parseJSON!(immutable(char)[]).parseJSON(immutable(char)[], int, std.json.JSONOptions).skipWhitespace()
378142 2199763 1819030 4 pure @property @safe dchar std.range.primitives.front!(immutable(char)).front(immutable(char)[])
120448 4223189 1521014 12 pure @safe void std.array.Appender!(immutable(char)[]).Appender.put!(dchar).put(dchar)
121018 1447202 1447202 11 _D3std5array17__T8AppenderTAyaZ8Appender11__T3putTAaZ3putMFAaZ10bigDataFunMFNaNbNemZAa
121018 2663475 1216273 10 pure nothrow @safe void std.array.Appender!(immutable(char)[]).Appender.put!(char[]).put(char[])
378144 429423 429423 1 pure @trusted dchar std.utf.decode!(0, const(immutable(char)[])).decode(ref const(immutable(char)[]), ref ulong)
378142 380732 380732 1 pure @trusted dchar std.utf.decode!(0, immutable(char)[]).decode(ref immutable(char)[], ref ulong)
3844 1585268 379140 98 _D3std4json18__T9parseJSONTAyaZ9parseJSONFAyaiE3std4json11JSONOptionsZ10parseValueMFKS3std4json9JSONValueZ11readIntegerMFNaNfZv
17692 3044067 299545 16 pure @safe bool std.json.parseJSON!(immutable(char)[]).parseJSON(immutable(char)[], int, std.json.JSONOptions).testChar!(true, true).testChar(char)
3694 302920 283609 76 pure @safe ulong std.conv.parse!(ulong, immutable(char)[]).parse(ref immutable(char)[])
21172 5305573 253537 11 pure @safe void std.json.parseJSON!(immutable(char)[]).parseJSON(immutable(char)[], int, std.json.JSONOptions).checkChar!(true, true).checkChar(char)
2858 241132 241132 84 _D3std5stdio4File17LockingTextWriter10__T3putTaZ3putMFNfaZ12trustedFPUTCFNbNiNeiPS4core4stdc5stdio7__sFILEZi
749606 194721 194721 0 pure nothrow @property @nogc @safe bool std.range.primitives.empty!(char).empty(const(char[]))
180 146178 146178 812 nothrow @nogc @trusted int std.format.formatValue!(std.array.Appender!(immutable(char)[]).Appender, float, char).formatValue(std.array.Appender!(immutable(char)[]).Appender, float, ref std.format.FormatSpec!(char).FormatSpec).__lambda6()
168966 289542 140496 0 pure @safe dchar std.json.parseJSON!(immutable(char)[]).parseJSON(immutable(char)[], int, std.json.JSONOptions).getChar!(false).getChar()
372 175283 138897 373 @safe bool std.format.FormatSpec!(char).FormatSpec.writeUpToNextSpec!(std.stdio.File.LockingTextWriter).writeUpToNextSpec(std.stdio.File.LockingTextWriter)
378142 127665 127665 0 pure nothrow @nogc @trusted void std.range.primitives.popFront!(immutable(char)).popFront(ref immutable(char)[])
120838 88309 88309 0 pure @safe ulong std.utf.encode!(0).encode(ref char[4], dchar)
7004 269543 79286 11 pure @safe void std.json.parseJSON!(immutable(char)[]).parseJSON(immutable(char)[], int, std.json.JSONOptions).checkChar!(false, false).checkChar(char)
60 9448603 75423 1257 @safe void redbus.print_item!(std.json.JSONValue).print_item(std.json.JSONValue)
180 357433 73536 408 @safe immutable(char)[] std.conv.toStr!(immutable(char)[], float).toStr(float)
11694 1701071 65993 5 pure @safe dchar std.json.parseJSON!(immutable(char)[]).parseJSON(immutable(char)[], int, std.json.JSONOptions).getChar!(true).getChar()
62 546598 58901 950 @safe uint std.format.formattedWrite!(std.stdio.File.LockingTextWriter, char, immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[]).formattedWrite(std.stdio.File.LockingTextWriter, const(char[]), immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[])
624 57414 57414 92 nothrow @nogc @trusted ulong std.stdio.trustedFwrite!(char).trustedFwrite(shared(core.stdc.stdio.__sFILE)*, const(char[]))
310 287767 55386 178 @safe void std.format.formatRange!(std.stdio.File.LockingTextWriter, immutable(char)[], char).formatRange(ref std.stdio.File.LockingTextWriter, ref immutable(char)[], ref std.format.FormatSpec!(char).FormatSpec)
180 283897 51186 284 @safe void std.format.formatValue!(std.array.Appender!(immutable(char)[]).Appender, float, char).formatValue(std.array.Appender!(immutable(char)[]).Appender, float, ref std.format.FormatSpec!(char).FormatSpec)
2792 188331 44715 16 @safe void std.range.primitives.put!(std.stdio.File.LockingTextWriter, char).put(ref std.stdio.File.LockingTextWriter, char)
3786 51453 42260 11 pure @safe bool std.json.parseJSON!(immutable(char)[]).parseJSON(immutable(char)[], int, std.json.JSONOptions).testChar!(false, false).testChar(char)
1 42812121 40946 40946 _Dmain
2792 143615 40695 14 @safe void std.range.primitives.doPut!(std.stdio.File.LockingTextWriter, char).doPut(ref std.stdio.File.LockingTextWriter, ref char)
2858 276528 35396 12 @safe void std.stdio.File.LockingTextWriter.put!(char).put(char)
62 730778 28544 460 @safe void std.stdio.File.writefln!(char, immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[]).writefln(const(char[]), immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[])
58 27284 25878 446 pure @safe double std.conv.parse!(double, immutable(char)[]).parse(ref immutable(char)[])
4 61182 22565 5641 @safe void std.stdio.writeln!(immutable(char)[]).writeln(immutable(char)[])
668 36701 22086 33 pure nothrow @safe void std.array.Appender!(immutable(char)[]).Appender.put!(char).put(char)
10790 15562 15562 1 pure nothrow @nogc @safe dchar std.ascii.toLower!(dchar).toLower(dchar)
180 14091 14091 78 pure nothrow @nogc @trusted immutable(char)[] std.algorithm.searching.find!("a == b", immutable(char)[], char).find(immutable(char)[], char).trustedMemchr(ref immutable(char)[], ref char)
668 14165 12798 19 pure nothrow @nogc @safe void std.conv.emplaceRef!(char, char, char).emplaceRef(ref char, ref char)
310 307336 11476 37 void std.format.formatGeneric!(std.stdio.File.LockingTextWriter, immutable(char)[], char).formatGeneric(std.stdio.File.LockingTextWriter, const(void)*, ref std.format.FormatSpec!(char).FormatSpec)
62 741889 11111 179 @safe void std.stdio.writefln!(immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[]).writefln(immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[])
60 17581 10002 166 pure @safe void std.algorithm.iteration.__T12FilterResultS246redbus4mainFZ9__lambda1TAS3std4json9JSONValueZ.FilterResult.popFront()
314 51833 9902 31 @safe void std.stdio.File.LockingTextWriter.put!(immutable(char)[]).put(immutable(char)[])
310 30175 9395 30 @safe void std.range.primitives.doPut!(std.stdio.File.LockingTextWriter, const(char)[]).doPut(ref std.stdio.File.LockingTextWriter, ref const(char)[])
310 40073 8883 28 @safe void std.range.primitives.doPut!(std.stdio.File.LockingTextWriter, immutable(char)[]).doPut(ref std.stdio.File.LockingTextWriter, ref immutable(char)[])
180 23372 8492 47 pure @safe immutable(char)[] std.algorithm.searching.find!("a == b", immutable(char)[], char).find(immutable(char)[], char)
310 295860 8093 26 @safe void std.format.formatValue!(std.stdio.File.LockingTextWriter, immutable(char)[], char).formatValue(std.stdio.File.LockingTextWriter, immutable(char)[], ref std.format.FormatSpec!(char).FormatSpec)
16860 7657 7657 0 pure nothrow @nogc @safe void std.range.primitives.popFront!(immutable(ubyte)).popFront(ref immutable(ubyte)[])
180 9026 7524 41 pure nothrow @nogc @safe bool std.functional.safeOp!("<").safeOp!(immutable(int), ulong).safeOp(ref immutable(int), ref ulong)
2 110576 6987 3493 void redbus.print_header()
17034 6217 6217 0 pure nothrow ref @property @nogc @safe immutable(ubyte) std.range.primitives.front!(immutable(ubyte)).front(immutable(ubyte)[])
120 5515 5515 45 pure nothrow ref @property @nogc @safe std.json.JSONValue std.range.primitives.front!(std.json.JSONValue).front(std.json.JSONValue[])
122 5635 5441 44 pure nothrow @property @nogc @safe bool std.algorithm.iteration.__T12FilterResultS246redbus4mainFZ9__lambda1TAS3std4json9JSONValueZ.FilterResult.empty()
310 20779 5296 17 @safe void std.stdio.File.LockingTextWriter.put!(const(char)[]).put(const(char)[])
310 34821 4646 14 @safe void std.range.primitives.put!(std.stdio.File.LockingTextWriter, const(char)[]).put(ref std.stdio.File.LockingTextWriter, const(char)[])
180 13549 4523 25 pure nothrow @nogc @safe immutable(int) std.algorithm.comparison.min!(immutable(int), ulong).min(immutable(int), ulong)
2 5331 4353 2176 nothrow @nogc @safe std.internal.cstring.tempCString!(char, immutable(char)[]).tempCString(immutable(char)[]).Res std.internal.cstring.tempCString!(char, immutable(char)[]).tempCString(immutable(char)[])
180 365563 4136 22 @safe immutable(char)[] std.conv.to!(immutable(char)[]).to!(float).to(float)
130 4682 4089 31 pure nothrow @nogc @safe float std.conv.to!(float).to!(long).to(long)
180 361427 3994 22 @safe immutable(char)[] std.conv.toImpl!(immutable(char)[], float).toImpl(float)
310 44049 3975 12 @safe void std.range.primitives.put!(std.stdio.File.LockingTextWriter, immutable(char)[]).put(ref std.stdio.File.LockingTextWriter, immutable(char)[])
3786 3530 3530 0 pure nothrow @nogc @safe immutable(char)[] std.string.assumeUTF!(immutable(ubyte)).assumeUTF(immutable(ubyte)[])
34 3566 3268 96 pure @safe long std.conv.parse!(long, immutable(char)[]).parse(ref immutable(char)[])
60 4002 2877 47 pure nothrow ref @property @nogc @safe std.json.JSONValue std.algorithm.iteration.__T12FilterResultS246redbus4mainFZ9__lambda1TAS3std4json9JSONValueZ.FilterResult.front()
3786 2775 2775 0 pure nothrow @nogc @safe immutable(ubyte)[] std.string.representation!(immutable(char)).representation(immutable(char)[])
50 2790 2342 46 pure nothrow @nogc @safe float std.conv.to!(float).to!(double).to(double)
180 1501 1501 8 pure nothrow @nogc @safe bool std.functional.safeOp!("<").unsafeOp!(immutable(int), ulong).unsafeOp(immutable(int), ulong)
184 940 940 5 pure nothrow @property @nogc @safe bool std.range.primitives.empty!(std.json.JSONValue).empty(const(std.json.JSONValue[]))
60 919 919 15 pure nothrow @nogc @safe void std.range.primitives.popFront!(std.json.JSONValue).popFront(ref std.json.JSONValue[])
58 833 833 14 pure nothrow @nogc @safe ubyte std.ascii.toUpper!(immutable(ubyte)).toUpper(immutable(ubyte))
2 6607022 831 415 @safe immutable(char)[] std.file.readText!(immutable(char)[], immutable(char)[]).readText(immutable(char)[])
180 787 787 4 pure nothrow @nogc @safe bool std.utf.canSearchInCodeUnits!(char).canSearchInCodeUnits(dchar)
62 779 779 12 pure nothrow @nogc @trusted pure nothrow @safe void function(std.stdio.File.LockingTextWriter, const(void)*, ref std.format.FormatSpec!(char).FormatSpec)* std.format.formattedWrite!(std.stdio.File.LockingTextWriter, char, immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[]).formattedWrite(std.stdio.File.LockingTextWriter, const(char[]), immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[]).__lambda9()
60 690 690 11 pure @safe bool redbus.main().__lambda1!(std.json.JSONValue).__lambda1(std.json.JSONValue)
62 668 668 10 pure nothrow @nogc @trusted const(void*) std.format.formattedWrite!(std.stdio.File.LockingTextWriter, char, immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[]).formattedWrite(std.stdio.File.LockingTextWriter, const(char[]), immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[]).__lambda14!(immutable(char)[]).__lambda14(return ref immutable(char)[])
130 593 593 4 pure nothrow @nogc @safe float std.conv.toImpl!(float, long).toImpl(long)
62 574 574 9 pure nothrow @nogc @trusted const(void*) std.format.formattedWrite!(std.stdio.File.LockingTextWriter, char, immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[]).formattedWrite(std.stdio.File.LockingTextWriter, const(char[]), immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[]).__lambda16!(immutable(char)[]).__lambda16(return ref immutable(char)[])
668 517 517 0 pure nothrow @nogc @trusted void std.conv.emplaceInitializer!(std.conv.emplaceRef!(char, char, char).emplaceRef(ref char, ref char).S).emplaceInitializer(ref std.conv.emplaceRef!(char, char, char).emplaceRef(ref char, ref char).S)
62 494 494 7 pure nothrow @nogc @trusted pure nothrow @safe void function(std.stdio.File.LockingTextWriter, const(void)*, ref std.format.FormatSpec!(char).FormatSpec)* std.format.formattedWrite!(std.stdio.File.LockingTextWriter, char, immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[]).formattedWrite(std.stdio.File.LockingTextWriter, const(char[]), immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[]).__lambda15()
62 494 494 7 pure nothrow @nogc @trusted pure nothrow @safe void function(std.stdio.File.LockingTextWriter, const(void)*, ref std.format.FormatSpec!(char).FormatSpec)* std.format.formattedWrite!(std.stdio.File.LockingTextWriter, char, immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[]).formattedWrite(std.stdio.File.LockingTextWriter, const(char[]), immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[]).__lambda17()
668 479 479 0 pure nothrow @nogc @trusted std.conv.emplaceRef!(char, char, char).emplaceRef(ref char, ref char).S* std.conv.emplaceRef!(char, char, char).emplaceRef(ref char, ref char).__lambda5()
668 449 449 0 _D3std5array17__T8AppenderTAyaZ8Appender10__T3putTaZ3putMFaZ9__lambda2MFNaNbNiNeZAa
50 447 447 8 pure nothrow @nogc @safe float std.conv.toImpl!(float, double).toImpl(double)
2 26578112 437 218 @safe std.json.JSONValue std.json.parseJSON!(immutable(char)[]).parseJSON(immutable(char)[], int, std.json.JSONOptions)
62 423 423 6 pure nothrow @nogc @trusted const(void*) std.format.formattedWrite!(std.stdio.File.LockingTextWriter, char, immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[]).formattedWrite(std.stdio.File.LockingTextWriter, const(char[]), immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[]).__lambda18!(immutable(char)[]).__lambda18(return ref immutable(char)[])
62 422 422 6 pure nothrow @nogc @trusted const(void*) std.format.formattedWrite!(std.stdio.File.LockingTextWriter, char, immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[]).formattedWrite(std.stdio.File.LockingTextWriter, const(char[]), immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[]).__lambda10!(immutable(char)[]).__lambda10(return ref immutable(char)[])
62 416 416 6 pure nothrow @nogc @trusted pure nothrow @safe void function(std.stdio.File.LockingTextWriter, const(void)*, ref std.format.FormatSpec!(char).FormatSpec)* std.format.formattedWrite!(std.stdio.File.LockingTextWriter, char, immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[]).formattedWrite(std.stdio.File.LockingTextWriter, const(char[]), immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[]).__lambda11()
62 416 416 6 pure nothrow @nogc @trusted pure nothrow @safe void function(std.stdio.File.LockingTextWriter, const(void)*, ref std.format.FormatSpec!(char).FormatSpec)* std.format.formattedWrite!(std.stdio.File.LockingTextWriter, char, immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[]).formattedWrite(std.stdio.File.LockingTextWriter, const(char[]), immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[]).__lambda13()
62 385 385 6 pure nothrow @nogc @trusted const(void*) std.format.formattedWrite!(std.stdio.File.LockingTextWriter, char, immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[]).formattedWrite(std.stdio.File.LockingTextWriter, const(char[]), immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[], immutable(char)[]).__lambda12!(immutable(char)[]).__lambda12(return ref immutable(char)[])
668 370 370 0 pure nothrow ref @nogc @safe std.conv.emplaceRef!(char, char, char).emplaceRef(ref char, ref char).S std.conv.emplaceRef!(char, char, char).emplaceRef(ref char, ref char).S.__ctor(ref char)
2 386 348 174 pure ref @safe std.algorithm.iteration.__T12FilterResultS246redbus4mainFZ9__lambda1TAS3std4json9JSONValueZ.FilterResult std.algorithm.iteration.__T12FilterResultS246redbus4mainFZ9__lambda1TAS3std4json9JSONValueZ.FilterResult.__ctor(std.json.JSONValue[])
2 583 343 171 pure nothrow @nogc @safe std.utf.byCodeUnit!(const(char)[]).byCodeUnit(const(char)[]).ByCodeUnitImpl std.utf.byUTF!(char).byUTF!(const(char)[]).byUTF(const(char)[])
亚当的回答:
➜ D ldc2 -O3 -release redbus.d
➜ D time ./redbus > /dev/null
./redbus > /dev/null 0.01s user 0.00s system 80% cpu 0.021 total
➜ D time ./redbus > /dev/null
./redbus > /dev/null 0.01s user 0.00s system 81% cpu 0.021 total
➜ D time ./redbus > /dev/null
./redbus > /dev/null 0.01s user 0.00s system 82% cpu 0.023 total
➜ D time ./redbus > /dev/null
./redbus > /dev/null 0.01s user 0.00s system 81% cpu 0.021 total
➜ D perl -ne 'print if /========/...//' trace.log | grep get_number
120 2249 2249 18 float redbus.get_number(std.json.JSONValue)
原始运行例外:
➜ D time ./redbus > /dev/null
./redbus > /dev/null 0.01s user 0.01s system 40% cpu 0.039 total
➜ D time ./redbus > /dev/null
./redbus > /dev/null 0.01s user 0.00s system 80% cpu 0.023 total
➜ D time ./redbus > /dev/null
./redbus > /dev/null 0.01s user 0.00s system 64% cpu 0.023 total
➜ D perl -ne 'print if /========/...//' trace.log | grep get_number
120 4971306 4966534 41387 pure @safe float redbus.get_number!(std.json.JSONValue).get_number(std.json.JSONValue)
答案 0 :(得分:1)
我认为只需将get_number
更改为此将有助于提高速度:
float get_number(JSONValue item) {
float output = 0;
if(item.type == JSON_TYPE.FLOAT)
output = item.floating;
else if(item.type == JSON_TYPE.INTEGER)
output = item.integer;
return output;
}
你能测试一下并让我知道吗?
编辑:在评论中,您询问了为什么个人资料差异并未完全反映在time
差异中。但是,它是!这个单独的功能现在快了1000倍......但它开始时已经相当快,所以最终并没有太大的差异。
这里的评论解释了为什么你期待的比你应该做的更多:
配置文件跟踪中给出的时间是 micro 秒,而不是毫秒,它们之间有1000x的差异。跟踪显示我的答案与原始代码之间的时间差约为40,000微秒......或40毫秒,或0.04秒。考虑到分析本身增加了一些开销,你不会看到最终二进制文件中的完整0.04秒变化....这与你的数字相匹配得足够好,平均而言我的答案给你0.02s的提升(或20,000微秒!),这也很接近匹配给定的分析开销。