R.Tanaka.Ichiro's Blog

主にC# な話題です

目次

Blog 利用状況

ニュース

イベントに空デリゲートを仕込ませてみる

http://blogs.wankuma.com/nakamura/archive/2008/11/25/162049.aspx
イベントを発生させるメソッドの実装(2)(Nakamura Blogより)

上記を読んで、面白いなー、と思って僕も試してみました。


public class Sample {
  public event EventHandler<HelloEventArgs> Hello = delegate {};
  protected virtual void OnHello(HelloEventArgs e) {
    Hello(this, e); // null にならないからチェックが不要
  }
}


確かに、インスタンス上から Hello を null する術がないということで、イベント発行時に null チェックが不要になる点では、すっきり書くことができますね。

また、毎回比較演算するよりは高速に処理できる筈・・・

でも、イベントの登録が 1 件増えるから、常に空の呼び出しが 1 件増えることとトレードオフな関係ですよね?
さて、どちらが高速なのでしょうか?

自分のメソッド内で、null 比較演算をする方が速いのか、イベントの空呼び出しによるオーバーヘッドの方が遅いのか?w


class TestClass {
  static void Method() {
    var t = new System.Diagnostics.Stopwatch();
    ISample[] a = { new Sample1(), new Sample2() };
    foreach(var i in Enumerable.Range(1, 5)) {
      Console.WriteLine(i.ToString() + "回目のテスト-----");
      foreach(var x in a) {
        x.Hello += (sender, e) => {};
        t.Reset();
        t.Start();
        foreach(var j in Enumerable.Range(0, 10000000)) x.Fire();
        t.Stop();
        Console.WriteLine(t.ElapsedTicks);
      }
    }
  }
}

public interface ISample {
  event EventHandler<EventArgs> Hello;
  void Fire();
}

public class Sample1 : ISample {
  public event EventHandler<EventArgs> Hello = delegate {};
  public void Fire() {
    Hello(this, EventArgs.Empty);
  }
}
public class Sample2 : ISample {
  public event EventHandler<EventArgs> Hello;
  public void Fire() {
    if (Hello != null) Hello(this, EventArgs.Empty);
  }
}


1回目のテスト-----
18016811
11843868
2回目のテスト-----
19669451
18374921
3回目のテスト-----
22624583
21159129
4回目のテスト-----
25875112
24878342
5回目のテスト-----
29027337
27788239


null の比較演算を毎回行う方が僅かながら速いんですね。
まあ、殆ど気にならないくらいですけど。

どちらの書き方が良いのかは、ちょっと結論ができませんが、僕は今まで通り後者でいこうかなー

投稿日時 : 2008年11月25日 14:10

Feedback

# re: イベントに空デリゲートを仕込ませてみる 2008/11/25 14:47 黒龍

空呼び出しの発生は失念してました。ちゃんと試さなきゃですね…。

# re: イベントに空デリゲートを仕込ませてみる 2008/11/25 15:32 R・田中一郎

興味深い方法だったので、僕としても明確なメリットがあるなら使ってみたいと思っています。

そういう意味で、きちんと結論を出しておきたくて、nakamura さんにも、失礼ながらずけずけと質問させていただきました。

実際、速度の面で有利であるかもしれない意見はありがたかったです。

# re: イベントに空デリゲートを仕込ませてみる 2008/11/25 16:10 なかむら

私もRさんと同じで、普段は後者(null比較)で書いています。
今回は「こういうやり方もあるんだなぁ」とちょっと関心したので紹介してみました。

# re: イベントに空デリゲートを仕込ませてみる 2008/11/25 19:59 R・田中一郎

とても興味深い情報をありがとうございました。
もうちょっと遊んでみます。

# re: イベントに空デリゲートを仕込ませてみる 2008/11/26 0:17 れい

コードの綺麗さとか長さっていうのはちょっとずつ、確実に効いてくるボディーブロー(?)のようなものですよね。

例えば、高速化のために「ループの展開」をする場合もありますが、展開しすぎるとコードがメモリを圧迫してトータルで遅くなる場合も多いわけで。

デリゲートがnullかどうかを比較してから呼び出すのも同じで、
それだけを単純ループで比較したら空呼び出しを入れておく方が遅いでしょうが、
現実のアプリケーションで使った場合、どちらが速いかは微妙であろうと思います。

こういうことは非常に多くて、実際にやってみないとわからない、というのは困ります。
大抵の場合はこうしておけばいい、みたいなことを偉い人が言ってくれるといいのですが。

誰も何も言ってくれないので、私はとりあえず「短いコードはトータルでは結構早い」という方針を取っています。
同じ意味ならキータイプの少ないコードを選ぶ。

なので、イベント入れたり出したりするようなコードを書く時は空呼び出しを最初に入れておく派です。

# re: イベントに空デリゲートを仕込ませてみる 2008/11/26 8:31 渋木宏明(ひどり)

速度以外の面も評価するべきです。

(見た目の)コードの複雑度に注目すると

・条件判断が一つ減る
・代入が一つ増える

ことになります。

微妙な差ですが、代入よりも条件判断の方が複雑度は↑名はずなので、コードの複雑度も僅かながら軽減していると思います。

# re: イベントに空デリゲートを仕込ませてみる 2008/11/26 12:27 なちゃ

若干別の観点だと、nullチェックするやり方が既に一般的に広まっているため、
nullチェックが無いのが正しいのか間違いなのか分かりにくい、
初心者がはじめにこちらをみると、単にnullチェックがいらないと誤解する可能性がある、
ってのもちょっとあったりします。

# re: イベントに空デリゲートを仕込ませてみる 2008/11/26 13:13 R・田中一郎

れい さん
渋木宏明(ひどり) さん

速度の面は、メリットもデメリットもないと考えて良いと思いますね。
実際、メソッドアウトすると速度が遅くなるという前提でプログラミングする人は殆どいない訳で、速度の検証をしたのは逆にそれを明らかにするという目的もありました。

また、継承などの理由で、イベントの着火は On~ で行う流儀があるので、null 判定を書いても 1 メソッドだけに包めてしまう点で、コードがきれいに書けることも、あまりメリットがないのかなー、とも思ったりしてます。

コードの書き方として、そのメソッドの独立性を重視した場合は、直前で null 判定する書き方の方が確実です。
という意味で、僕は null かなーと思ってますけど、うーむ、悩むなぁ・・・

# re: イベントに空デリゲートを仕込ませてみる 2008/11/26 13:15 R・田中一郎

なちゃ さん

それもありますよね。
イベントが初期化されているかどうかを確認しないといけないですもんね。

# re: イベントに空デリゲートを仕込ませてみる 2008/11/26 18:16 渋木宏明(ひどり)

>そのメソッドの独立性

も、ほんの僅かな差ですね。

着火メソッドの実装は、どーあってもイベント本体?に依存しちゃうわけで。

# re: イベントに空デリゲートを仕込ませてみる 2008/11/27 23:39 R・田中一郎

そうかなぁ。

以下のコードがあったとしますよね。

public event EventHandler<EventArgs> Hello = delegate {};
public void Fire() {
  Hello(this, EventArgs.Empty);
}

この一部がコンストラクタに書かれている場合よりは、上記のようにまとまっている方が・・・

これも僅かな差ですねw

# イベントに空デリゲートを仕込ませてみる(その2) 2008/11/28 14:29 R.Tanaka.Ichiro's Blog

イベントに空デリゲートを仕込ませてみる(その2)

# イベントに空デリゲートを仕込ませてみる(その2) 2008/12/02 15:27 R.Tanaka.Ichiro's Blog

イベントに空デリゲートを仕込ませてみる(その2)

# Right away I am ready to do my breakfast, once having my breakfast coming again to read other news. 2019/04/09 1:55 Right away I am ready to do my breakfast, once hav

Right away I am ready to do my breakfast, once having my breakfast coming again to read other news.

# Hi! I know this is somewhat off-topic however I had to ask. Does running a well-established website like yours require a massive amount work? I am brand new to writing a blog however I do write in my journal on a daily basis. I'd like to start a blog s 2019/05/17 11:54 Hi! I know this is somewhat off-topic however I ha

Hi! I know this is somewhat off-topic however I had to ask.
Does running a well-established website like yours require a massive amount work?
I am brand new to writing a blog however I do write in my journal on a
daily basis. I'd like to start a blog so I can easily share my
experience and thoughts online. Please let me know if you have any kind of
ideas or tips for new aspiring blog owners. Thankyou!

# Spot on with this write-up, I actually believe that this site needs much more attention. I'll probably be back again to read through more, thanks for the information! 2019/06/15 0:55 Spot on with this write-up, I actually believe tha

Spot on with this write-up, I actually believe that this site needs much more attention. I'll probably be back again to read through more, thanks
for the information!

# This paragraph provides clear idea in favor of the new viewers of blogging, that truly how to do blogging. 2019/07/19 16:22 This paragraph provides clear idea in favor of the

This paragraph provides clear idea in favor of the new viewers of
blogging, that truly how to do blogging.

# This paragraph provides clear idea in favor of the new viewers of blogging, that truly how to do blogging. 2019/07/19 16:23 This paragraph provides clear idea in favor of the

This paragraph provides clear idea in favor of the new viewers of
blogging, that truly how to do blogging.

# Yes! Finally someone writes about plenty of fish dating site. 2019/07/25 18:12 Yes! Finally someone writes about plenty of fish

Yes! Finally someone writes about plenty of fish dating site.

# Yes! Finally someone writes about plenty of fish dating site. 2019/07/25 18:13 Yes! Finally someone writes about plenty of fish

Yes! Finally someone writes about plenty of fish dating site.

# Yes! Finally someone writes about plenty of fish dating site. 2019/07/25 18:14 Yes! Finally someone writes about plenty of fish

Yes! Finally someone writes about plenty of fish dating site.

# Yes! Finally someone writes about plenty of fish dating site. 2019/07/25 18:15 Yes! Finally someone writes about plenty of fish

Yes! Finally someone writes about plenty of fish dating site.

# I am regular reader, how are you everybody? This paragraph posted at this site is in fact pleasant. 2019/08/15 9:50 I am regular reader, how are you everybody? This p

I am regular reader, how are you everybody? This paragraph posted at this site is in fact pleasant.

# Illikebuisse gvims 2021/07/03 2:16 pharmacepticacom

sildenafil citrate over the counter https://pharmaceptica.com/

# sFQJeLBysCNVxG 2021/07/03 2:48 https://amzn.to/365xyVY

Your style is very unique in comparison to other people I have read stuff from. I appreciate you for posting when you ave got the opportunity, Guess I all just book mark this web site.

# aZhkfUvKWgDchA 2021/07/03 4:17 https://www.blogger.com/profile/060647091882378654

Thanks so much for the post.Much thanks again. Much obliged.

# Thanks for finally writing about >イベントに空デリゲートを仕込ませてみる <Liked it! 2021/07/04 3:39 Thanks for finally writing about >イベントに空デリゲートを仕

Thanks for finally writing about >イベントに空デリゲートを仕込ませてみる <Liked it!

# re: ??????????????????? 2021/07/16 6:07 hydroxychloroquine safe

chloroquine phosphate tablet https://chloroquineorigin.com/# hydrochloquine

# re: ??????????????????? 2021/08/09 16:53 antimalarial drug hydroxychloroquine

aralen for lupus https://chloroquineorigin.com/# what does hydroxychloroquine treat

# ivermectin 6 http://stromectolabc.com/
ivermectin 50 mg 2022/02/07 18:19 Busjdhj

ivermectin 6 http://stromectolabc.com/
ivermectin 50 mg

# buy ivermectin nz http://stromectolabc.com/
ivermectin 3 2022/02/08 10:42 Busjdhj

buy ivermectin nz http://stromectolabc.com/
ivermectin 3

# buy doxycycline online without prescription https://doxycyline1st.com/
doxycycline 100mg price 2022/02/25 22:56 Doxycycline

buy doxycycline online without prescription https://doxycyline1st.com/
doxycycline 100mg price

# doxycycline 200 mg https://doxycyline1st.com/
doxycycline hyc 100mg 2022/02/26 10:10 Doxycycline

doxycycline 200 mg https://doxycyline1st.com/
doxycycline hyc 100mg

# Hello There. I found your weblog the use of msn. That is an extremely well written article. I will make sure to bookmark it and come back to read more of your helpful info. Thanks for the post. I'll definitely comeback. 2022/03/23 1:53 Hello There. I found your weblog the use of msn.

Hello There. I found your weblog the use of msn. That
is an extremely well written article. I will make sure to bookmark it and
come back to read more of your helpful info.
Thanks for the post. I'll definitely comeback.

# Hello There. I found your weblog the use of msn. That is an extremely well written article. I will make sure to bookmark it and come back to read more of your helpful info. Thanks for the post. I'll definitely comeback. 2022/03/23 1:54 Hello There. I found your weblog the use of msn.

Hello There. I found your weblog the use of msn. That
is an extremely well written article. I will make sure to bookmark it and
come back to read more of your helpful info.
Thanks for the post. I'll definitely comeback.

# Hello There. I found your weblog the use of msn. That is an extremely well written article. I will make sure to bookmark it and come back to read more of your helpful info. Thanks for the post. I'll definitely comeback. 2022/03/23 1:55 Hello There. I found your weblog the use of msn.

Hello There. I found your weblog the use of msn. That
is an extremely well written article. I will make sure to bookmark it and
come back to read more of your helpful info.
Thanks for the post. I'll definitely comeback.

# Hello There. I found your weblog the use of msn. That is an extremely well written article. I will make sure to bookmark it and come back to read more of your helpful info. Thanks for the post. I'll definitely comeback. 2022/03/23 1:56 Hello There. I found your weblog the use of msn.

Hello There. I found your weblog the use of msn. That
is an extremely well written article. I will make sure to bookmark it and
come back to read more of your helpful info.
Thanks for the post. I'll definitely comeback.

# It's a shame you don't have a donate button! I'd definitely donate to this outstanding blog! I suppose for now i'll settle for book-marking and adding your RSS feed to my Google account. I look forward to new updates and will talk about this site with m 2022/03/23 14:45 It's a shame you don't have a donate button! I'd d

It's a shame you don't have a donate button! I'd definitely donate to this outstanding blog!
I suppose for now i'll settle for book-marking and adding your RSS feed to
my Google account. I look forward to new updates and will talk about this site with my Facebook group.

Chat soon!

# It's a shame you don't have a donate button! I'd definitely donate to this outstanding blog! I suppose for now i'll settle for book-marking and adding your RSS feed to my Google account. I look forward to new updates and will talk about this site with m 2022/03/23 14:47 It's a shame you don't have a donate button! I'd d

It's a shame you don't have a donate button! I'd definitely donate to this outstanding blog!
I suppose for now i'll settle for book-marking and adding your RSS feed to
my Google account. I look forward to new updates and will talk about this site with my Facebook group.

Chat soon!

# It's a shame you don't have a donate button! I'd definitely donate to this outstanding blog! I suppose for now i'll settle for book-marking and adding your RSS feed to my Google account. I look forward to new updates and will talk about this site with m 2022/03/23 14:48 It's a shame you don't have a donate button! I'd d

It's a shame you don't have a donate button! I'd definitely donate to this outstanding blog!
I suppose for now i'll settle for book-marking and adding your RSS feed to
my Google account. I look forward to new updates and will talk about this site with my Facebook group.

Chat soon!

# It's a shame you don't have a donate button! I'd definitely donate to this outstanding blog! I suppose for now i'll settle for book-marking and adding your RSS feed to my Google account. I look forward to new updates and will talk about this site with m 2022/03/23 14:49 It's a shame you don't have a donate button! I'd d

It's a shame you don't have a donate button! I'd definitely donate to this outstanding blog!
I suppose for now i'll settle for book-marking and adding your RSS feed to
my Google account. I look forward to new updates and will talk about this site with my Facebook group.

Chat soon!

# Wonderful, what a webpage it is! This web site presents useful data to us, keep it up. 2022/03/24 3:20 Wonderful, what a webpage it is! This web site pre

Wonderful, what a webpage it is! This web site presents useful data to us,
keep it up.

# Wonderful, what a webpage it is! This web site presents useful data to us, keep it up. 2022/03/24 3:21 Wonderful, what a webpage it is! This web site pre

Wonderful, what a webpage it is! This web site presents useful data to us,
keep it up.

# Wonderful, what a webpage it is! This web site presents useful data to us, keep it up. 2022/03/24 3:22 Wonderful, what a webpage it is! This web site pre

Wonderful, what a webpage it is! This web site presents useful data to us,
keep it up.

# Wonderful, what a webpage it is! This web site presents useful data to us, keep it up. 2022/03/24 3:23 Wonderful, what a webpage it is! This web site pre

Wonderful, what a webpage it is! This web site presents useful data to us,
keep it up.

# What's up colleagues, how is everything, and what you desire to say on the topic of this paragraph, in my view its actually amazing in favor of me. 2022/03/25 0:57 What's up colleagues, how is everything, and what

What's up colleagues, how is everything, and what you desire to say on the topic of this paragraph,
in my view its actually amazing in favor of me.

# What's up colleagues, how is everything, and what you desire to say on the topic of this paragraph, in my view its actually amazing in favor of me. 2022/03/25 0:58 What's up colleagues, how is everything, and what

What's up colleagues, how is everything, and what you desire to say on the topic of this paragraph,
in my view its actually amazing in favor of me.

# What's up colleagues, how is everything, and what you desire to say on the topic of this paragraph, in my view its actually amazing in favor of me. 2022/03/25 0:59 What's up colleagues, how is everything, and what

What's up colleagues, how is everything, and what you desire to say on the topic of this paragraph,
in my view its actually amazing in favor of me.

# What's up colleagues, how is everything, and what you desire to say on the topic of this paragraph, in my view its actually amazing in favor of me. 2022/03/25 1:00 What's up colleagues, how is everything, and what

What's up colleagues, how is everything, and what you desire to say on the topic of this paragraph,
in my view its actually amazing in favor of me.

# srdunnaiaiwv 2022/06/03 2:03 ktninnms

http://erythromycin1m.com/# erythromycin brand

# ihjdggtaipld 2022/06/14 16:31 egnmgoze

plaquenil otc http://www.hydroxychloroquinex.com/

# ed treatments https://erectionpills.best/
cheapest ed pills 2022/06/28 11:29 ErectionPills

ed treatments https://erectionpills.best/
cheapest ed pills

# corona tabletten https://paxlovid.best/
paxlovid generic 2022/09/07 23:18 Paxlovid

corona tabletten https://paxlovid.best/
paxlovid generic

# I love what you guys are usually up too. Such clever work and exposure! Keep up the terrific works guys I've incorporated you guys to my own blogroll. 2022/11/30 20:00 I love what you guys are usually up too. Such clev

I love what you guys are usually up too.
Such clever work and exposure! Keep up the terrific works guys I've incorporated you
guys to my own blogroll.

# hydroxychloroquine 200mg for sale 2022/12/25 19:12 MorrisReaks

http://hydroxychloroquinex.com/ generic plaquenil 200mg

# versandapotheke 2023/09/26 15:26 Williamreomo

http://onlineapotheke.tech/# online apotheke versandkostenfrei
versandapotheke deutschland

# п»їonline apotheke 2023/09/26 23:09 Williamreomo

https://onlineapotheke.tech/# online apotheke gГ?nstig
internet apotheke

# online apotheke deutschland 2023/09/27 0:05 Williamreomo

https://onlineapotheke.tech/# internet apotheke
internet apotheke

# п»їonline apotheke 2023/09/27 3:53 Williamreomo

http://onlineapotheke.tech/# online apotheke preisvergleich
internet apotheke

# online apotheke gГјnstig 2023/09/27 13:10 Williamreomo

http://onlineapotheke.tech/# gГ?nstige online apotheke
versandapotheke

# farmacie online sicure 2023/09/27 18:28 Rickeyrof

acheter sildenafil 100mg sans ordonnance

# farmacie on line spedizione gratuita 2023/09/27 19:31 Rickeyrof

acheter sildenafil 100mg sans ordonnance

# acquistare farmaci senza ricetta 2023/09/27 20:21 Rickeyrof

acheter sildenafil 100mg sans ordonnance

# online canadian pharmacies 2023/10/16 14:52 Dannyhealm

Their international drug database is unparalleled. http://mexicanpharmonline.shop/# mexican rx online

# buy prescription online 2023/10/16 19:31 Dannyhealm

I'm grateful for their around-the-clock service. https://mexicanpharmonline.com/# mexican mail order pharmacies

# buying pharmaceuticals from canada 2023/10/16 20:36 Dannyhealm

They always prioritize the customer's needs. http://mexicanpharmonline.com/# mexican mail order pharmacies

# online mexican pharmacies 2023/10/16 21:20 Dannyhealm

Their international insights have benefited me greatly. https://mexicanpharmonline.shop/# mexico drug stores pharmacies

# canadian pills online 2023/10/16 22:01 Dannyhealm

Their health and beauty section is fantastic. https://mexicanpharmonline.com/# mexico drug stores pharmacies

# approved canadian pharmacies 2023/10/17 6:38 Dannyhealm

Their patient education resources are top-tier. http://mexicanpharmonline.shop/# mexican border pharmacies shipping to usa

# top rated canadian pharmacies 2023/10/17 8:20 Dannyhealm

Read now. https://mexicanpharmonline.com/# mexican mail order pharmacies

# cheap prescription medication online 2023/10/17 10:35 Dannyhealm

They offer unparalleled advice on international healthcare. https://mexicanpharmonline.shop/# mexican rx online

# reputable canadian pharmacies online 2023/10/17 23:04 Dannyhealm

Impressed with their dedication to international patient care. http://mexicanpharmonline.com/# mexican pharmaceuticals online

# no prescription on line pharmacies 2023/10/18 8:13 Dannyhealm

I'm always informed about potential medication interactions. http://mexicanpharmonline.com/# mexican pharmaceuticals online

# buy rx online 2023/10/18 12:54 Dannyhealm

The widest range of international brands under one roof. https://mexicanpharmonline.shop/# mexico drug stores pharmacies

# valtrex over the counter uk https://valtrex.auction/ generic valtrex from india 2023/10/25 1:54 Valtrex

valtrex over the counter uk https://valtrex.auction/ generic valtrex from india

# paxlovid cost without insurance https://paxlovid.bid/ paxlovid covid 2023/10/26 2:45 Paxlovid

paxlovid cost without insurance https://paxlovid.bid/ paxlovid covid

# ブルガリ時計 レプリカ ヴィンテージ 2023/11/07 21:06 bjbyjef@msn.com

ヴィトンのバッグを購入。
中古ランクAの商品でしたが、実物を見てびっくり
これで中古?と驚いてしまいました。
とても綺麗で未使用品と言われても分からないくらいのバックです。
想像を遥かに上回る美品で、妻も大喜びしてくれました。
ネットで中古品を購入するのははじめてのことでしたが、信頼できるお店に出会えて嬉しかったです。
次回もこちらのお店を利用したいと思います。
ブルガリ時計 レプリカ ヴィンテージ https://www.bagtojapan.com/product/3737.htm

# best pill for ed 2023/11/22 4:20 WilliamApomb

http://tadalafil.trade/# tadalafil 20 mg best price

# buy doxycycline online 270 tabs https://doxycycline.forum/ order doxycycline 2023/11/25 16:34 Doxycycline

buy doxycycline online 270 tabs https://doxycycline.forum/ order doxycycline

# top online canadian pharmacies 2023/11/29 21:48 MichaelBum

https://clomid.club/# can i purchase generic clomid tablets

# farmacie online autorizzate elenco https://farmaciait.pro/ migliori farmacie online 2023 2023/12/04 12:55 Farmacia

farmacie online autorizzate elenco https://farmaciait.pro/ migliori farmacie online 2023

# gГјnstige online apotheke 2023/12/19 6:14 FrankTic

https://apotheke.company/# online apotheke deutschland

タイトル
名前
Url
コメント