c#: practical duty bid even faster nominee invocation?
it only happens me formula settlement question. say, i have "template" slight invokes functions competence "alter". discerning settlement follow "template settlement pattern". interpretation altering functions "virtual" functions overridden subclasses. or, i only nominee functions but "virtual". nominee functions injected customized too.
originally, i guess second "delegate" proceed faster "virtual" way, nonetheless coding dash proves correct.
in next code, initial dosomething slight follows "template pattern". calls practical slight istokenchar. second dosomthing slight doesn't count practical function. instead, pass-in delegate. computer, initial dosomthing always faster second. outcome 1645:1780.
"virtual invocation" enterprising contracting should some-more time-costing proceed elect invocation, right? nonetheless outcome shows not.
anybody explain this?
using system;
using system.diagnostics;
class foo
{
open practical bool istokenchar(string word)
{
relapse string.isnullorempty(word);
}
// template method
open int dosomething(string word)
{
int truecount = 0;
(int i = 0; i < repeat; ++i)
{
(istokenchar(word))
{
++truecount;
}
}
relapse truecount;
}
open int dosomething(predicate<string> predicator, twine word)
{
int truecount = 0;
(int i = 0; i < repeat; ++i)
{
(predicator(word))
{
++truecount;
}
}
relapse truecount;
}
private int repeat = 200000000;
}
class program
{
stationary vacant main(string[] args)
{
foo f = new foo();
{
stopwatch sw = stopwatch.startnew();
f.dosomething(null);
sw.stop();
console.writeline(sw.elapsedmilliseconds);
}
{
stopwatch sw = stopwatch.startnew();
f.dosomething(str => string.isnullorempty(str), null);
sw.stop();
console.writeline(sw.elapsedmilliseconds);
}
}
}
Comments
Post a Comment