I need some help with attribute
Welp I don't know much about attribute and GPT keeps sending me s*** code
Anyone can give me an example on how to do this:
When we call a function with a attribute [timerTest]
It will print out how long does the function runs
11 Replies
Do you want to do code benchmarks?
If so, use BenchmarkDotNet
Otherwise, the magic is not in the attribute. It is only used to mark parts of your code.
Implementing what you want might be possible via partial methods and source generators, I guess. But I haven't researched that yet
Oh
this sounds like it's potentially a job for interceptors
but those are experimental
True
basically this is a lot more complicated to do than you think it is
it would be easier to just wrap the call in a stopwatch or use an actual benchmarking framework like $bdn
Try BenchmarkDotNet if you want a sophisticated library for benchmarking https://github.com/dotnet/BenchmarkDotNet
But again, if you want to bench your code, use a well tested library
Like the one mentioned above
Thanks this seems like what I'm looking for
it's definitely what you want if you want accurate measurements, calling a method once won't get you a realistic execution time because of the JIT and progressive optimization
$close
Use the /close command to mark a forum thread as answered