|
Revision 13, 0.8 kB
(checked in by BCS, 2 years ago)
|
added Witold Baryluk's iterators
|
| Line | |
|---|
| 1 |
import std.stdio; |
|---|
| 2 |
|
|---|
| 3 |
import iterators; |
|---|
| 4 |
|
|---|
| 5 |
class Numbers { |
|---|
| 6 |
int n; |
|---|
| 7 |
this (int n0) { |
|---|
| 8 |
n = n0; |
|---|
| 9 |
} |
|---|
| 10 |
|
|---|
| 11 |
int iter() { |
|---|
| 12 |
for (int i = 0; i < n; i++) { |
|---|
| 13 |
yield(i); |
|---|
| 14 |
} |
|---|
| 15 |
return 0; |
|---|
| 16 |
} |
|---|
| 17 |
mixin mainiter!(int, iter); |
|---|
| 18 |
} |
|---|
| 19 |
|
|---|
| 20 |
int main(char[][] args) { |
|---|
| 21 |
const int TESTS = 40; |
|---|
| 22 |
|
|---|
| 23 |
const int n = 1_000_000; |
|---|
| 24 |
|
|---|
| 25 |
{ |
|---|
| 26 |
auto t = new Timer(); |
|---|
| 27 |
for (int j = 0; j < TESTS; j++) { |
|---|
| 28 |
long sum = 0; |
|---|
| 29 |
for (int i = 0; i < n; i++) { |
|---|
| 30 |
sum += i*j; |
|---|
| 31 |
} |
|---|
| 32 |
//writefln(sum); |
|---|
| 33 |
} |
|---|
| 34 |
} |
|---|
| 35 |
|
|---|
| 36 |
auto s = new Numbers(n); |
|---|
| 37 |
|
|---|
| 38 |
{ |
|---|
| 39 |
auto t = new Timer(); |
|---|
| 40 |
for (int j = 0; j < TESTS; j++) { |
|---|
| 41 |
long sum = 0; |
|---|
| 42 |
foreach (int i; s) { |
|---|
| 43 |
sum += i*j; |
|---|
| 44 |
} |
|---|
| 45 |
//writefln(sum); |
|---|
| 46 |
} |
|---|
| 47 |
} |
|---|
| 48 |
|
|---|
| 49 |
return 0; |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
import std.date; |
|---|
| 54 |
|
|---|
| 55 |
class Timer { |
|---|
| 56 |
static d_time getCount(){return getUTCtime();} |
|---|
| 57 |
d_time starttime; |
|---|
| 58 |
this() { starttime = getCount(); } |
|---|
| 59 |
~this() { writefln("elapsed time = %s", (getCount() - starttime)/1000.0); } |
|---|
| 60 |
} |
|---|