c# - Query related to getting number of days in between of two days -


i need number of days between 2 given dates.

i used "(enddate-startdate).days" senior advised may incur memory leaks, told me may result in different values if used on 32 bit system vs on 64 bit system , advised me use timespan instead , days.

i couldn't point.

can please explain how may incur memory management issue?

datetime dt1 = datetime.now; datetime dt2 = datetime.now.adddays(-10); int daysdiff = (dt2-dt1).days; // i'm doing this.  timespan ts = dt2-dt1; // senior- int daysdiff = ts.days. // advised me. 

please correct me if i'm wrong i'm not proficient on point. in advance, appreciate help.

c# code:

    int yourversion()     {         datetime dt1 = datetime.now;         datetime dt2 = datetime.now.adddays(-10);         int daysdiff = (dt2 - dt1).days;         return daysdiff;     }     int seniorversion()     {         datetime dt1 = datetime.now;         datetime dt2 = datetime.now.adddays(-10);         timespan ts = dt2 - dt1;         int daysdiff = ts.days;         return daysdiff;     } 

compiled on vs2013, release build, decompiled il:

yours:

.method private hidebysig instance int32          yourversion() cil managed {   // code size       48 (0x30)   .maxstack  2   .locals init ([0] valuetype [mscorlib]system.datetime dt1,            [1] valuetype [mscorlib]system.datetime dt2,            [2] int32 daysdiff,            [3] valuetype [mscorlib]system.datetime cs$0$0000,            [4] valuetype [mscorlib]system.timespan cs$0$0001)   il_0000:  call       valuetype [mscorlib]system.datetime [mscorlib]system.datetime::get_now()   il_0005:  stloc.0   il_0006:  call       valuetype [mscorlib]system.datetime [mscorlib]system.datetime::get_now()   il_000b:  stloc.3   il_000c:  ldloca.s   cs$0$0000   il_000e:  ldc.r8     -10.   il_0017:  call       instance valuetype [mscorlib]system.datetime [mscorlib]system.datetime::adddays(float64)   il_001c:  stloc.1   il_001d:  ldloc.1   il_001e:  ldloc.0   il_001f:  call       valuetype [mscorlib]system.timespan [mscorlib]system.datetime::op_subtraction(valuetype [mscorlib]system.datetime,                                                                                                      valuetype [mscorlib]system.datetime)   il_0024:  stloc.s    cs$0$0001   il_0026:  ldloca.s   cs$0$0001   il_0028:  call       instance int32 [mscorlib]system.timespan::get_days()   il_002d:  stloc.2   il_002e:  ldloc.2   il_002f:  ret } // end of method program::yourversion 

your senior's:

.method private hidebysig instance int32          seniorversion() cil managed {   // code size       48 (0x30)   .maxstack  2   .locals init ([0] valuetype [mscorlib]system.datetime dt1,            [1] valuetype [mscorlib]system.datetime dt2,            [2] valuetype [mscorlib]system.timespan ts,            [3] int32 daysdiff,            [4] valuetype [mscorlib]system.datetime cs$0$0000)   il_0000:  call       valuetype [mscorlib]system.datetime [mscorlib]system.datetime::get_now()   il_0005:  stloc.0   il_0006:  call       valuetype [mscorlib]system.datetime [mscorlib]system.datetime::get_now()   il_000b:  stloc.s    cs$0$0000   il_000d:  ldloca.s   cs$0$0000   il_000f:  ldc.r8     -10.   il_0018:  call       instance valuetype [mscorlib]system.datetime [mscorlib]system.datetime::adddays(float64)   il_001d:  stloc.1   il_001e:  ldloc.1   il_001f:  ldloc.0   il_0020:  call       valuetype [mscorlib]system.timespan [mscorlib]system.datetime::op_subtraction(valuetype [mscorlib]system.datetime,                                                                                                      valuetype [mscorlib]system.datetime)   il_0025:  stloc.2   il_0026:  ldloca.s   ts   il_0028:  call       instance int32 [mscorlib]system.timespan::get_days()   il_002d:  stloc.3   il_002e:  ldloc.3   il_002f:  ret } // end of method program::seniorversion 

the difference? yours has compiler generated timespan variable used hold result of dt2 - dt1, , senior's has named variable. other that, they're same code.

there's no risk of memory leak, , code run same on 32-bit , 64-bit systems.

(there few minor differences based on specific instructions used access local variables, these not expected produce perturbations in code jit can produce)


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

node.js - Node - Passport Auth - Authed Post Route hangs on form submission -

Does Firefox offer AppleScript support to get URL of windows? -