简体   繁体   中英

C# How do you monitor accesses to array elements?

Arrays are all of type Array , instead of their underlying type, meaning making arrays out of your own custom primitives with event handling and stuff is useless. Someone on Discord said that it'll probably take either reflection or unsafe constructs. Adding code to the get accessor is useless, because Array.Sort() only calls that accessor twice. What should I do instead to call code like events whenever an array element is accessed (like read or written)?

Here's what I'm trying to make, a benchmarker for sorting algorithms that charts the number of comparisons and total array accesses on a whole range of array sizes Sort benchmarker

Don't use arrays for your experiments, use IList<T> instead. This should allow you to write a decorator that does whatever logging you want to do. Or write your own custom IArray<T> interface.

But note that benchmarking is complicated, while logging the number of compares/swaps could be useful, the overall performance will depend on things like how expensive each operation is for your particular type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM