Reading array of integers readln performance issues
Dennis Ritchie via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Jun 11 13:33:42 PDT 2015
On Thursday, 11 June 2015 at 19:56:00 UTC, kerdemdemir wrote:
> Hi;
>
> To learn D better and challanging myself I am tring code
> computation's with D.
>
> There is a question which is about reading a line of integer
> which consist of 200000 elements.
>
> My solution fails because "Time limit exceeded", I thought it
> is because of my algorithm first. I realize time limit is
> exceeded even before my algorithm starts while reading line of
> integers. I understand this by giving a wrong answer to
> question after readln statement. I did that to get a "wrong
> answer error" but my code still get a "Time limit exceed" error
> because "readln" takes very long time.
>
> Can I achieve something faster than code below?
>
> auto peopleMoney = stdin.readln().split().map!(a =>
> to!int(a)).array();
> if (peopleMoney.length == 200000)
> writeln(":(");
>
> Regards
> Erdem
>
>
> Ps: I do not want to bore you with long code, but I am sending
> link to whole program anyway if anyone need.
> http://codeforces.com/contest/549/submission/11537206
Your algorithm works for about quadratic time. For N = 200000
your algorithm will work terribly long. The function `readln()`
nothing to do with:
http://codeforces.com/contest/549/submission/11476513
A faster way of `scanf`, but it will not help you, because your
algorithm is slow.
More information about the Digitalmars-d-learn
mailing list