Created
February 1, 2011 03:55
-
-
Save JesseKPhillips/805392 to your computer and use it in GitHub Desktop.
Build a CSV file for testing performance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv; | |
import std.file; | |
void main() { | |
auto data = readText("sample.csv"); | |
//auto records = csvText!Layout(data); | |
auto records = csvText!Layout(data, ["a", "b", "c", "d", "e"]); | |
foreach(r; records) | |
{ | |
} | |
} | |
struct Layout { | |
string a; | |
string b; | |
int c; | |
string d; | |
float e; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csvRange; | |
import std.file; | |
void main() { | |
auto data = readText("sample.csv"); | |
auto records = csvStructRange!Layout(csvFile(data, ','), ["a", "b", "c", "d", "e"]); | |
foreach(r; records) | |
{ | |
} | |
} | |
struct Layout { | |
string a; | |
string b; | |
int c; | |
string d; | |
float e; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std.stdio; | |
import std.string; | |
import std.algorithm; | |
import std.random; | |
import std.file; | |
import std.traits; | |
import std.exception; | |
import std.typecons; | |
import std.conv; | |
struct Layout { | |
string a; | |
string b; | |
int c; | |
string d; | |
float e; | |
} | |
auto head = "a,b,c,d,e"; | |
void main(string[] args) { | |
enforce(args.length > 2, "Usage: " ~ args[0] ~ " minfilesize file"); | |
auto words = readText("words.english").splitlines; | |
auto size = to!int(args[1]); | |
std.file.write(args[2], head ~ "\n"); | |
Layout data; | |
while(getSize(args[2]) < size) { | |
foreach(i, U; FieldTypeTuple!Layout) { | |
static if(is(U == string)) | |
data.tupleof[i] = buildString(words); | |
else static if(is(U == int)) | |
data.tupleof[i] = uniform(0,words.length); | |
else static if(is(U == float)) | |
data.tupleof[i] = uniform(0,10)/15.0 * uniform(0,words.length); | |
} | |
writeStruct(args[2], data); | |
} | |
} | |
string buildString(string[] words) { | |
auto wordCount = uniform(1,7); | |
string result; | |
while(wordCount > 0) { | |
scope(exit) wordCount--; | |
result ~= words[uniform(0,words.length)]; | |
switch(dice([90,10,2,2,1])) { | |
case 0: | |
result ~= " "; | |
break; | |
case 1: | |
result ~= ", "; | |
break; | |
case 2: | |
result ~= "\" "; | |
break; | |
case 3: | |
result ~= " \""; | |
break; | |
case 4: | |
result ~= "\n"; | |
break; | |
default: | |
break; | |
} | |
} | |
return result[0..$-1]; | |
} | |
void writeStruct(string file, Layout data) { | |
string[] elements; | |
foreach(i, U; FieldTypeTuple!Layout) { | |
elements ~= to!string(data.tupleof[i]); | |
} | |
string record; | |
foreach(e; elements) { | |
if(find(e, `"`, ",", "\n", "\r")[1] == 0) | |
record ~= e ~ ","; | |
else | |
record ~= `"` ~ replace(e, `"`, `""`) ~ `",`; | |
} | |
std.file.append(file, record[0..$-1] ~ "\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment