Click here to Skip to main content
15,916,683 members
Home / Discussions / C#
   

C#

 
AnswerRe: MultiThreading, went back to basics.. Pin
jas0n2326-Nov-08 18:07
jas0n2326-Nov-08 18:07 
QuestionVMR9 issue [modified] Pin
GrizzlyDoug26-Nov-08 7:09
GrizzlyDoug26-Nov-08 7:09 
AnswerRe: VMR9 issue Pin
MBrooker7-Mar-09 6:23
MBrooker7-Mar-09 6:23 
QuestionTry...Catch block Pin
EliottA26-Nov-08 6:54
EliottA26-Nov-08 6:54 
AnswerRe: Try...Catch block Pin
PIEBALDconsult26-Nov-08 7:29
mvePIEBALDconsult26-Nov-08 7:29 
AnswerRe: Try...Catch block Pin
Gideon Engelberth26-Nov-08 7:33
Gideon Engelberth26-Nov-08 7:33 
AnswerRe: Try...Catch block Pin
Paul Conrad26-Nov-08 9:23
professionalPaul Conrad26-Nov-08 9:23 
GeneralRe: Try...Catch block [modified] Pin
Luc Pattyn26-Nov-08 10:31
sitebuilderLuc Pattyn26-Nov-08 10:31 
Hi,

the try statement pushes some information to the execution stack; this info gets popped again when the try block closes without exceptions; or the stack gets searched for it when an exception occurs (that is the purpose of the info). So a try statement has a small cost associated with it.

Manual nesting of try blocks will have a limited depth, and typically only the inner one will be relevant; now there is no way around if you want to catch a possible exception at that level (e.g. because you want to continue with the next iteration of the inner loop).

And you can't easily nest try blocks programmatically (if would take run-time code generation to do that), unless you have a recursive method. Which is the way to go if you want to measure the try cost: write a recursive program containing a stopwatch and run it; then add a try-catch to the recursive method and measure it again. You probably will not see a significant difference, the recursion is much more costly than the try statement itself.

Here is some test code:
public override void Test(int arg) {
    long rep=100000;
    long n=1000;
    Stopwatch sw=new Stopwatch();
    sw.Start();
    for (int i=0; i<rep; i++) sw.Stop();
    log(sw.ElapsedMilliseconds.ToString());
    Stopwatch sw2=new Stopwatch();
    sw2.Start();
    for (int i=0; i<rep; i++) sw2.Stop();
    log(sw2.ElapsedMilliseconds.ToString());
}

long fact(long n) {
    if (n<=1) return 1;
    return n*fact(n-1);
}

long fact2(long n) {
    try {
        if (n<=1) return 1;
        return n*fact2(n-1);
    } catch (Exception) {
        return 0;
    }
}


Smile | :)

Luc Pattyn [Forum Guidelines] [My Articles]

Fixturized forever. Confused | :confused:


modified on Wednesday, November 26, 2008 4:39 PM

QuestionSearching for URL's in... Pin
jas0n2326-Nov-08 6:19
jas0n2326-Nov-08 6:19 
AnswerRe: Searching for URL's in... Pin
Christian Graus26-Nov-08 7:44
protectorChristian Graus26-Nov-08 7:44 
QuestionRe: Searching for URL's in... Pin
jas0n2326-Nov-08 16:32
jas0n2326-Nov-08 16:32 
QuestionLinq - Inserting into IDENTITY column. Pin
Exception Duck26-Nov-08 5:52
Exception Duck26-Nov-08 5:52 
QuestionHTTPContext for Remote object. (i didn't find Remoting forum) Pin
Member 232448326-Nov-08 5:47
Member 232448326-Nov-08 5:47 
QuestionConfigurationSettings Pin
madhu.nzd26-Nov-08 4:21
madhu.nzd26-Nov-08 4:21 
QuestionAssertions Pin
CodingYoshi26-Nov-08 4:05
CodingYoshi26-Nov-08 4:05 
AnswerRe: Assertions Pin
User 665826-Nov-08 5:32
User 665826-Nov-08 5:32 
Questionneed help translating one certain line of code from c++ to c# Pin
mSh198526-Nov-08 3:24
mSh198526-Nov-08 3:24 
AnswerRe: need help translating one certain line of code from c++ to c# Pin
HosamAly26-Nov-08 3:39
HosamAly26-Nov-08 3:39 
GeneralRe: need help translating one certain line of code from c++ to c# Pin
mSh198526-Nov-08 4:10
mSh198526-Nov-08 4:10 
GeneralRe: need help translating one certain line of code from c++ to c# Pin
HosamAly26-Nov-08 4:18
HosamAly26-Nov-08 4:18 
Questionclass or struct Pin
Harvey Saayman26-Nov-08 2:46
Harvey Saayman26-Nov-08 2:46 
AnswerRe: class or struct Pin
Thomas Weller26-Nov-08 3:06
Thomas Weller26-Nov-08 3:06 
AnswerRe: class or struct Pin
Jan Sommer26-Nov-08 3:09
Jan Sommer26-Nov-08 3:09 
GeneralRe: class or struct Pin
Thomas Weller26-Nov-08 4:15
Thomas Weller26-Nov-08 4:15 
GeneralRe: class or struct Pin
Jan Sommer26-Nov-08 4:31
Jan Sommer26-Nov-08 4:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.