-
Notifications
You must be signed in to change notification settings - Fork 5
Closed
Description
They said explicitly that version 5 dropped Append and used manual iteration through the fields. That version benchmark at 1.1 seconds, similar to your Nim code. While they never specify the entire version 5, they did say what it was. Basically version 2 with the associative array and loop exit. I've edited version two with those changes. I have not compile it yet so do compile with LDC or correct any mistakes I made.
https://dlang.org/blog/2017/05/24/faster-command-line-tools-in-d/
int main(string[] args)
{
import std.algorithm : max, maxElement, splitter;
import std.conv : to;
import std.range : enumerate;
import std.stdio;
if (args.length < 4)
{
writefln ("synopsis: %s filename keyfield valuefield", args[0]);
return 1;
}
string filename = args[1];
size_t keyFieldIndex = args[2].to!size_t;
size_t valueFieldIndex = args[3].to!size_t;
size_t maxFieldIndex = max(keyFieldIndex, valueFieldIndex);
string delim = "\t";
long[string] sumByKey;
foreach(line; filename.File.byLine)
{
string key;
long value;
bool allFound = false;
foreach (i, field; line.splitter(delim).enumerate)
{
if (i == keyFieldIndex) key = field.to!string;
if (i == valueFieldIndex) value = field.to!long;
if (i == maxFieldIndex)
{
allFound = true;
if (auto sumValuePtr = key in sumByKey) *sumValuePtr += value;
else sumByKey[key] = value;
break;
}
}
}
if (sumByKey.length == 0) writeln("No entries");
else
{
auto maxEntry = sumByKey.byKeyValue.maxElement!"a.value";
writeln("max_key: ", maxEntry.key, " sum: ", maxEntry.value);
}
return 0;
}
Metadata
Metadata
Assignees
Labels
No labels