Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
24
Task/Time-a-function/D/time-a-function-1.d
Normal file
24
Task/Time-a-function/D/time-a-function-1.d
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import std.stdio, std.datetime;
|
||||
|
||||
int identity(int x) {
|
||||
return x;
|
||||
}
|
||||
|
||||
int sum(int num) {
|
||||
foreach (i; 0 .. 100_000_000)
|
||||
num += i;
|
||||
return num;
|
||||
}
|
||||
|
||||
double timeIt(int function(int) func, int arg) {
|
||||
StopWatch sw;
|
||||
sw.start();
|
||||
func(arg);
|
||||
sw.stop();
|
||||
return sw.peek().usecs / 1_000_000.0;
|
||||
}
|
||||
|
||||
void main() {
|
||||
writefln("identity(4) takes %f6 seconds.", timeIt(&identity, 4));
|
||||
writefln("sum(4) takes %f seconds.", timeIt(&sum, 4));
|
||||
}
|
||||
27
Task/Time-a-function/D/time-a-function-2.d
Normal file
27
Task/Time-a-function/D/time-a-function-2.d
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import tango.io.Stdout;
|
||||
import tango.time.Clock;
|
||||
|
||||
int identity (int x)
|
||||
{
|
||||
return x;
|
||||
}
|
||||
|
||||
int sum (int num)
|
||||
{
|
||||
for (int i = 0; i < 1000000; i++)
|
||||
num += i;
|
||||
return num;
|
||||
}
|
||||
|
||||
double timeIt(int function(int) func, int arg)
|
||||
{
|
||||
long before = Clock.now.ticks;
|
||||
func(arg);
|
||||
return (Clock.now.ticks - before) / cast(double)TimeSpan.TicksPerSecond;
|
||||
}
|
||||
|
||||
void main ()
|
||||
{
|
||||
Stdout.format("Identity(4) takes {:f6} seconds",timeIt(&identity,4)).newline;
|
||||
Stdout.format("Sum(4) takes {:f6} seconds",timeIt(&sum,4)).newline;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue