中の技術日誌ブログ

C#とC++/CLIと
VBと.NETとWindowsで戯れる
 

目次

Blog 利用状況

ニュース

自己紹介

東京でソフトウェアエンジニアをやっています。
お仕事大募集中です。
記事執筆や、講師依頼とかでも何でもどうぞ(*^_^*)
似顔絵 MSMVPロゴ
MSMVP Visual C# Since 2004/04-2013/03

記事カテゴリ

書庫

日記カテゴリ

00-整理

01-MSMVP

例外処理のオーバーヘッド C#

http://blogs.wankuma.com/andochin/archive/2009/04/27/172138.aspx

以下のことは思考実験くらいに思って下さいね。実際の業務で使うべきかどうかとはまた別の話。

static void null_check()
{
    object p = func1();
    if (p != null)
    {
        p = func2(p);
        if (p != null)
        {
            p = func3(p);
        }
    }
}
        static void null_check()
        {
            object p = func1();
00000000  sub         rsp,48h 
00000004  mov         qword ptr [rsp+20h],0 
0000000d  mov         rax,7FF001E1F28h 
00000017  mov         eax,dword ptr [rax] 
00000019  test        eax,eax 
0000001b  je          0000000000000022 
0000001d  call        FFFFFFFFF2DAEF00 
00000022  call        FFFFFFFFFFEC94F0 
00000027  mov         qword ptr [rsp+28h],rax 
0000002c  mov         rax,qword ptr [rsp+28h] 
00000031  mov         qword ptr [rsp+20h],rax 
            if (p != null)
00000036  cmp         qword ptr [rsp+20h],0 
0000003c  je          0000000000000078 
            {
                p = func2(p);
0000003e  mov         rcx,qword ptr [rsp+20h] 
00000043  call        FFFFFFFFFFEC94E8 
00000048  mov         qword ptr [rsp+30h],rax 
0000004d  mov         rax,qword ptr [rsp+30h] 
00000052  mov         qword ptr [rsp+20h],rax 
                if (p != null)
00000057  cmp         qword ptr [rsp+20h],0 
0000005d  je          0000000000000078 
                {
                    p = func3(p);
0000005f  mov         rcx,qword ptr [rsp+20h] 
00000064  call        FFFFFFFFFFEC94E0 
00000069  mov         qword ptr [rsp+38h],rax 
0000006e  mov         rax,qword ptr [rsp+38h] 
00000073  mov         qword ptr [rsp+20h],rax 
                }
            }
        }
private static void null_check2()
{
    try
    {
        object p = func1();
        p = func2(p);
        p = func3(p);
    }
    catch
    {
        throw new Exception();
        
    }
}
        private static void null_check2()

        {
            try
            {
                object p = func1();
00000000  push        rbp  
00000001  sub         rsp,60h 
00000005  lea         rbp,[rsp+20h] 
0000000a  mov         qword ptr [rbp],rsp 
0000000e  mov         qword ptr [rbp+8],0 
00000016  mov         rax,7FF001E1F28h 
00000020  mov         eax,dword ptr [rax] 
00000022  test        eax,eax 
00000024  je          000000000000002B 
00000026  call        FFFFFFFFF2DAF140 
0000002b  nop              
0000002c  call        FFFFFFFFFFEC9730 
00000031  mov         qword ptr [rbp+10h],rax 
00000035  mov         rax,qword ptr [rbp+10h] 
00000039  mov         qword ptr [rbp+8],rax 
                p = func2(p);
0000003d  mov         rcx,qword ptr [rbp+8] 
00000041  call        FFFFFFFFFFEC9728 
00000046  mov         qword ptr [rbp+18h],rax 
0000004a  mov         rax,qword ptr [rbp+18h] 
0000004e  mov         qword ptr [rbp+8],rax 
                p = func3(p);
00000052  mov         rcx,qword ptr [rbp+8] 
00000056  call        FFFFFFFFFFEC9720 
0000005b  mov         qword ptr [rbp+20h],rax 
0000005f  mov         rax,qword ptr [rbp+20h] 
00000063  mov         qword ptr [rbp+8],rax 
00000067  jmp         0000000000000069 
                
            }
        }

あきらかにCのコードに比べて汚いんだけど、どこをどう見てもNULLチェックしない分だけ2の方が速いですよ。

cmp         qword ptr [rsp+20h],0
je          0000000000000078

NULLチェックと言ってもここの部分ですけどね。

実際にこのコードではNULLならスルーしてるだけですが、例外の場合には例外をはき直してます。

それよりここ無駄なんじゃ・・・

00000048  mov         qword ptr [rsp+30h],rax 
0000004d  mov         rax,qword ptr [rsp+30h]

アセンブリもうちょっと勉強しないと。

投稿日時 : 2009年4月27日 9:07

コメントを追加

# re: 例外処理のオーバーヘッド C# 2009/04/27 10:13 あんどちん

C#の方がより例外発生しない場合は早そうですね。

僕のエントリで指摘されたとおり、例外発生コストの事を考えると一概には言えませんが、通常は例外補足で済ませたいな。
コードもすっきりするし。
# だがC++では極度に例外を嫌って使わないw

# そんなところにこだわってどうするの? じゃないよ! 2009/04/28 12:38 中の技術日誌ブログ

そんなところにこだわってどうするの? じゃないよ!

# When someone writes an post he/she retains the image of a user in his/her mind that how a user can be aware of it. Thus that's why this post is perfect. Thanks! 2019/05/05 0:54 When someone writes an post he/she retains the ima

When someone writes an post he/she retains the image of a
user in his/her mind that how a user can be aware of it.
Thus that's why this post is perfect. Thanks!

# Useful info. Fortunate me I discovered your website accidentally, and I'm stunned why this coincidence didn't took place earlier! I bookmarked it. 2021/08/18 4:24 Useful info. Fortunate me I discovered your websit

Useful info. Fortunate me I discovered your website accidentally,
and I'm stunned why this coincidence didn't took place earlier!

I bookmarked it.

# Wow, superb weblog structure! How long have you ever been blogging for? you make blogging glance easy. The entire glance of your web site is fantastic, as neatly as the content! 2021/08/30 13:03 Wow, superb weblog structure! How long have you ev

Wow, superb weblog structure! How long have you ever been blogging for?

you make blogging glance easy. The entire glance
of your web site is fantastic, as neatly as the content!

# Wow, superb weblog structure! How long have you ever been blogging for? you make blogging glance easy. The entire glance of your web site is fantastic, as neatly as the content! 2021/08/30 13:04 Wow, superb weblog structure! How long have you ev

Wow, superb weblog structure! How long have you ever been blogging for?

you make blogging glance easy. The entire glance
of your web site is fantastic, as neatly as the content!

# Wow, superb weblog structure! How long have you ever been blogging for? you make blogging glance easy. The entire glance of your web site is fantastic, as neatly as the content! 2021/08/30 13:05 Wow, superb weblog structure! How long have you ev

Wow, superb weblog structure! How long have you ever been blogging for?

you make blogging glance easy. The entire glance
of your web site is fantastic, as neatly as the content!

# Wow, superb weblog structure! How long have you ever been blogging for? you make blogging glance easy. The entire glance of your web site is fantastic, as neatly as the content! 2021/08/30 13:06 Wow, superb weblog structure! How long have you ev

Wow, superb weblog structure! How long have you ever been blogging for?

you make blogging glance easy. The entire glance
of your web site is fantastic, as neatly as the content!

# you are actually a excellent webmaster. The website loading pace is amazing. It kind of feels that you're doing any unique trick. In addition, The contents are masterwork. you have performed a magnificent task in this topic! 2021/09/04 19:25 you are actually a excellent webmaster. The websit

you are actually a excellent webmaster. The website loading pace is amazing.

It kind of feels that you're doing any unique trick.
In addition, The contents are masterwork. you have performed a
magnificent task in this topic!

# you are actually a excellent webmaster. The website loading pace is amazing. It kind of feels that you're doing any unique trick. In addition, The contents are masterwork. you have performed a magnificent task in this topic! 2021/09/04 19:26 you are actually a excellent webmaster. The websit

you are actually a excellent webmaster. The website loading pace is amazing.

It kind of feels that you're doing any unique trick.
In addition, The contents are masterwork. you have performed a
magnificent task in this topic!

# you are actually a excellent webmaster. The website loading pace is amazing. It kind of feels that you're doing any unique trick. In addition, The contents are masterwork. you have performed a magnificent task in this topic! 2021/09/04 19:27 you are actually a excellent webmaster. The websit

you are actually a excellent webmaster. The website loading pace is amazing.

It kind of feels that you're doing any unique trick.
In addition, The contents are masterwork. you have performed a
magnificent task in this topic!

# you are actually a excellent webmaster. The website loading pace is amazing. It kind of feels that you're doing any unique trick. In addition, The contents are masterwork. you have performed a magnificent task in this topic! 2021/09/04 19:28 you are actually a excellent webmaster. The websit

you are actually a excellent webmaster. The website loading pace is amazing.

It kind of feels that you're doing any unique trick.
In addition, The contents are masterwork. you have performed a
magnificent task in this topic!

# I like this web blog very much so much good information. 2021/09/06 20:59 I like this web blog very much so much good inform

I like this web blog very much so much good information.

# I go to see every day a few sites and blogs to read articles, except this web site presents feature based articles. 2021/09/13 9:07 I go to see every day a few sites and blogs to rea

I go to see every day a few sites and blogs to read articles, except this web site presents feature based articles.

# Thanks to my father who stated to me about this website, this web site is truly awesome. 2021/09/18 4:09 Thanks to my father who stated to me about this we

Thanks to my father who stated to me about this
website, this web site is truly awesome.

# I pay a quick visit everyday a few web sites and sites to read posts, however this weblog provides quality based content. 2021/09/23 6:19 I pay a quick visit everyday a few web sites and s

I pay a quick visit everyday a few web sites and sites to read posts, however this weblog provides quality based content.

# I pay a quick visit everyday a few web sites and sites to read posts, however this weblog provides quality based content. 2021/09/23 6:21 I pay a quick visit everyday a few web sites and s

I pay a quick visit everyday a few web sites and sites to read posts, however this weblog provides quality based content.

# I pay a quick visit everyday a few web sites and sites to read posts, however this weblog provides quality based content. 2021/09/23 6:22 I pay a quick visit everyday a few web sites and s

I pay a quick visit everyday a few web sites and sites to read posts, however this weblog provides quality based content.

# I pay a quick visit everyday a few web sites and sites to read posts, however this weblog provides quality based content. 2021/09/23 6:23 I pay a quick visit everyday a few web sites and s

I pay a quick visit everyday a few web sites and sites to read posts, however this weblog provides quality based content.

# Article writing is also a fun, if you be familiar with then you can write if not it is complicated to write. 2021/09/23 11:26 Article writing is also a fun, if you be familiar

Article writing is also a fun, if you be familiar with then you
can write if not it is complicated to write.

# I am just commenting to make you be aware of what a brilliant experience my wife's child obtained browsing yuor web blog. She mastered a lot of details, including how it is like to have an amazing helping nature to make men and women smoothly fully grasp 2021/09/23 12:23 I am just commenting to make you be aware of what

I am just commenting to make you be aware of what a brilliant experience my wife's child obtained browsing yuor
web blog. She mastered a lot of details, including how it is like to have an amazing helping nature to make men and women smoothly fully
grasp several complicated topics. You really surpassed our own expected
results. I appreciate you for imparting the necessary, trusted, explanatory and
unique tips on the topic to Gloria.

# I do not even understand how I finished up here, however I assumed this post was once great. I do not know who you're but certainly you're going to a famous blogger should you aren't already. Cheers! 2021/09/23 16:25 I do not even understand how I finished up here, h

I do not even understand how I finished up here, however I assumed this post was once
great. I do not know who you're but certainly you're going to a famous blogger should you aren't already.
Cheers!

# Post writing is also a fun, if you know then you can write otherwise it is complicated to write. 2021/10/01 6:53 Post writing is also a fun, if you know then you c

Post writing is also a fun, if you know then you can write otherwise it
is complicated to write.

# Post writing is also a fun, if you know then you can write otherwise it is complicated to write. 2021/10/01 6:55 Post writing is also a fun, if you know then you c

Post writing is also a fun, if you know then you can write otherwise it
is complicated to write.

# Post writing is also a fun, if you know then you can write otherwise it is complicated to write. 2021/10/01 6:56 Post writing is also a fun, if you know then you c

Post writing is also a fun, if you know then you can write otherwise it
is complicated to write.

# Post writing is also a fun, if you know then you can write otherwise it is complicated to write. 2021/10/01 6:57 Post writing is also a fun, if you know then you c

Post writing is also a fun, if you know then you can write otherwise it
is complicated to write.

# First of all I want to say awesome blog! I had a quick question in which I'd like to ask if you don't mind. I was curious to know how you center yourself and clear your thoughts before writing. I've had trouble clearing my mind in getting my thoughts out 2021/10/05 23:45 First of all I want to say awesome blog! I had a q

First of all I want to say awesome blog! I had a quick question in which I'd like to ask if you
don't mind. I was curious to know how you center yourself
and clear your thoughts before writing. I've had trouble clearing my mind in getting
my thoughts out there. I truly do enjoy writing but it just seems
like the first 10 to 15 minutes are usually lost simply
just trying to figure out how to begin. Any suggestions or tips?
Kudos!

# As the admin of this web page is working, no question very shortly it will be renowned, due to its quality contents. 2021/10/25 16:20 As the admin of this web page is working, no quest

As the admin of this web page is working, no question very shortly it will be renowned,
due to its quality contents.

# As the admin of this web page is working, no question very shortly it will be renowned, due to its quality contents. 2021/10/25 16:21 As the admin of this web page is working, no quest

As the admin of this web page is working, no question very shortly it will be renowned,
due to its quality contents.

# As the admin of this web page is working, no question very shortly it will be renowned, due to its quality contents. 2021/10/25 16:22 As the admin of this web page is working, no quest

As the admin of this web page is working, no question very shortly it will be renowned,
due to its quality contents.

# As the admin of this web page is working, no question very shortly it will be renowned, due to its quality contents. 2021/10/25 16:23 As the admin of this web page is working, no quest

As the admin of this web page is working, no question very shortly it will be renowned,
due to its quality contents.

# Hi, i think that i noticed you visited my weblog so i got here to ?go back the want?.I am trying to in finding things to enhance my web site!I assume its good enough to use some of your ideas!! 2021/12/22 10:23 Hi, i think that i noticed you visited my weblog s

Hi, i think that i noticed you visited my weblog
so i got here to ?go back the want?.I am trying to in finding things to
enhance my web site!I assume its good enough to use some of your ideas!!

# I visited several sites except the audio quality for audio songs existing at this website is actually fabulous. 2022/03/24 3:32 I visited several sites except the audio quality f

I visited several sites except the audio quality for audio songs existing
at this website is actually fabulous.

# With havin so much content and articles do you ever run into any issues of plagorism or copyright infringement? My blog has a lot of completely unique content I've either written myself or outsourced but it appears a lot of it is popping it up all over 2022/03/25 3:40 With havin so much content and articles do you eve

With havin so much content and articles do you ever run into any
issues of plagorism or copyright infringement?

My blog has a lot of completely unique content I've either written myself or outsourced but it appears a lot of it is popping it
up all over the internet without my authorization. Do you know any ways
to help prevent content from being ripped off? I'd definitely
appreciate it.

# Hi there, after reading this awesome piece of writing i am too glad to share my familiarity here with mates. 2022/11/30 12:41 Hi there, after reading this awesome piece of writ

Hi there, after reading this awesome piece of writing i
am too glad to share my familiarity here with mates.

# My partner and I stumbled over here different website and thought I should check things out. I like what I see so now i'm following you. Look forward to looking over your web page for a second time. 2022/12/01 23:11 My partner and I stumbled over here different web

My partner and I stumbled over here different website and thought I should
check things out. I like what I see so now i'm following you.
Look forward to looking over your web page for a second time.

# My partner and I stumbled over here different website and thought I should check things out. I like what I see so now i'm following you. Look forward to looking over your web page for a second time. 2022/12/01 23:11 My partner and I stumbled over here different web

My partner and I stumbled over here different website and thought I should
check things out. I like what I see so now i'm following you.
Look forward to looking over your web page for a second time.

# My partner and I stumbled over here different website and thought I should check things out. I like what I see so now i'm following you. Look forward to looking over your web page for a second time. 2022/12/01 23:12 My partner and I stumbled over here different web

My partner and I stumbled over here different website and thought I should
check things out. I like what I see so now i'm following you.
Look forward to looking over your web page for a second time.

# My partner and I stumbled over here different website and thought I should check things out. I like what I see so now i'm following you. Look forward to looking over your web page for a second time. 2022/12/01 23:13 My partner and I stumbled over here different web

My partner and I stumbled over here different website and thought I should
check things out. I like what I see so now i'm following you.
Look forward to looking over your web page for a second time.

# sweet bonanza nasıl oynanır https://sweetbonanza.bid/ - sweet bonanza kazanma saatleri
2024/03/27 19:45 Bonanzaj

sweet bonanza nasıl oynanır https://sweetbonanza.bid/ - sweet bonanza kazanma saatleri

タイトル
名前
URL
コメント