cmake-d/tests/app_7/app_7.d

76 lines
1.3 KiB
D
Raw Normal View History

2010-08-14 20:11:51 +00:00
import std.stdio;
import core.thread;
2011-02-22 13:53:28 +00:00
import std.datetime;
2010-08-14 20:11:51 +00:00
// Yield count should be larger for a
// more accurate measurment, but this
// is just a unit tests, so don't spin
// for long
immutable uint yield_count = 1000;
immutable uint worker_count = 10;
void fiber_func()
{
uint i = yield_count;
while( --i ) Fiber.yield();
}
void thread_func()
{
uint i = yield_count;
while( --i ) Thread.yield();
}
void fiber_test()
{
Fiber[worker_count] fib_array;
foreach( ref f; fib_array )
f = new Fiber( &fiber_func );
2011-02-22 13:53:28 +00:00
StopWatch sw;
2010-08-14 20:11:51 +00:00
uint i = yield_count;
// fibers are cooperative and need a driver loop
2011-02-22 13:53:28 +00:00
sw.start();
2010-08-14 20:11:51 +00:00
bool done;
do
{
done = true;
foreach( f; fib_array )
{
f.call();
if( f.state() != f.State.TERM )
done = false;
}
} while( !done );
2011-02-22 13:53:28 +00:00
sw.stop();
2010-08-14 20:11:51 +00:00
writeln( "Elapsed time for ", worker_count, " workers times ", yield_count, " yield() calls with fibers = ",
2011-02-22 13:53:28 +00:00
sw.peek().msecs, "ms" );
2010-08-14 20:11:51 +00:00
}
void thread_test()
{
Thread[worker_count] thread_array;
foreach( ref t; thread_array )
t = new Thread( &thread_func );
2011-02-22 13:53:28 +00:00
StopWatch sw;
sw.start();
2010-08-14 20:11:51 +00:00
foreach( t; thread_array )
t.start();
thread_joinAll();
2011-02-22 13:53:28 +00:00
sw.stop();
2010-08-14 20:11:51 +00:00
writeln( "Elapsed time for ", worker_count, " workers times ", yield_count, " yield() calls with threads = ",
2011-02-22 13:53:28 +00:00
sw.peek().msecs, "ms" );
2010-08-14 20:11:51 +00:00
}
int main()
{
fiber_test();
thread_test();
return 0;
}