前の日 / 次の日 / 最新 / 2010-02

01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

2010-02-23 Tue

if文の条件内で宣言したい [D言語]

宣言が構造体の変数だとどう書く?


import std.stdio;

struct S
{
   
int value;
}

void main()
{
   
if(auto s = S(10)) //Error: expression S(10) of type S does not have a boolean value
   
{
        writeln
(s.value);
   
}
}

こういう書き方ではないようだ。

クラスの場合はどうだろう。


import std.stdio;

class C
{
   
int value;
   
this(int value)
   
{
       
this.value = value;
   
}
}

void main()
{
   
if(auto c = new C(10))
   
{
        writeln
(c.value);
   
}
}

動いた。

構造体を new してみる。


import std.stdio;

struct S
{
   
int value;
   
this(int value)
   
{
       
this.value = value;
   
}
}

void main()
{
   
if(auto s = new S(10))
   
{
        writeln
(s.value);
   
}
}

動いた。

D言語で気軽に時間測定がしたい [D言語]

C++ で気軽に時間測定がしたい
D言語で書いてみる。

クラスで書いてみた。


import std.stdio;
import std.format;
import std.utf;
import std.perf;

scope class Benchmark(T=PerformanceCounter)
{
   
alias T.interval_t interval_t;
    T timer
;
   
char[] msg;


   
this(...)
   
{
       
void putc(dchar c)
       
{
            std
.utf.encode(msg, c);
       
}
        std
.format.doFormat(&putc, _arguments, _argptr);

        timer
= new T;
        timer
.start;
   
}

   
~this()
   
{
        timer
.stop;
        interval_t elapsedMsec
= timer.milliseconds;
        stderr
.writefln("%s: %s msec", msg, elapsedMsec);
   
}
   
}

スコープ変数にしてデストラクタが動くようにする。


void main()
{
   
{
       
int i = 10_000_000
       
scope benchmark = Benchmark!()("sleep(%d)",i);
        Thread
.sleep(i);
   
}
}
sleep(10000000): 1000 msec

構造体で書いてみる。


import std.stdio;
import std.format;
import std.utf;
import std.perf;

struct Benchmark(T=PerformanceCounter)
{
   
alias T.interval_t interval_t;
    T timer
;
   
char[] msg;
   
this(...)
   
{
       
void putc(dchar c)
       
{
            std
.utf.encode(msg, c);
       
}
        std
.format.doFormat(&putc, _arguments, _argptr);

        timer
= new T;
        timer
.start;
   
}
   
~this()
   
{
        timer
.stop;
        interval_t elapsedMsec
= timer.milliseconds;
        stderr
.writefln("%s: %s msec", msg, elapsedMsec);
   
}
}

void main()
{
   
{
       
int i = 10_000_000
       
auto benchmark = Benchmark!()("sleep(%d)",i);
        Thread
.sleep(i);
   
}
}
sleep(10000000): 1000 msec

これだと入れ子のたびに名前をつけなくてはいけない。
だから if 文の条件中に宣言しているのか?
結構難しい。

delegate を使って書いてみる。


import std.stdio;
import std.format;
import std.utf;
import std.perf;

void benchmark(T=PerformanceCounter)(void delegate() dg,...)
{
   
alias T.interval_t interval_t;
    T timer
= new T;
   
char[] msg;

   
void putc(dchar c)
   
{
        std
.utf.encode(msg, c);
   
}

    std
.format.doFormat(&putc, _arguments, _argptr);
    timer
.start;
    dg
();
    timer
.stop;
    interval_t elapsedMsec
= timer.milliseconds;
    stderr
.writefln("%s: %s msec", msg, elapsedMsec);
}

void main()
{
   
for(int i = 0; i <= 3; i++) {
        benchmark
({
            Thread
.sleep( i * 10_000_000 );
       
},"sleep(%d)", i * 10_000_000 );
   
}
}
sleep(0): 11 msec
sleep(10000000): 999 msec
sleep(20000000): 2004 msec
sleep(30000000): 3002 msec

けっこうそれっぽい?