Skip to content

Instantly share code, notes, and snippets.

@krwq
Last active April 22, 2022 11:50
Show Gist options
  • Save krwq/d9d1bad3d59ff30f8db2a53a27adc755 to your computer and use it in GitHub Desktop.
Save krwq/d9d1bad3d59ff30f8db2a53a27adc755 to your computer and use it in GitHub Desktop.
Setter perf comparison

This compares 4 different types of setter - first argument of a setter is always object on which property is declared. We're comparing different combinations on perf difference depending if declaring type is generic type or object. For the last (Func) case we return the object since structs will always operate on the copy which means you wouldn't be able to modify them otherwise.

  • Times in milliseconds.
  • Iterations per testcase: 500000000
  • Actionish<ref TDeclaringType, TPropertyType> - is actually a delegate equivalent to such action since you cannot actually use such type
  • Action<object, object> is a baseline for difference for other cases i.e. [-8.7%] meaning total runtime of equivalent test case was 8.7% shorter or in simpler words the more negative value the faster it is.
  • for first two cases modifying struct property has to happen using Unsafe.Unbox since simply casting to a struct would operate on its copy
TestCase Action<object, object> Action<object, TPropertyType> Actionish<ref TDeclaringType, TPropertyType> Func<TDeclaringType, TPropertyType, TDeclaringType>
TestClass.IntProperty 1862 1700 [-8.7%] 1346 [-27.71%] 1353 [-27.34%]
TestClass.IntField 1927 1636 [-15.1%] 1281 [-33.52%] 1286 [-33.26%]
TestClass.StringProperty 2242 2124 [-5.26%] 1780 [-20.61%] 1788 [-20.25%]
TestClass.StringField 2179 2213 [+1.56%] 1745 [-19.92%] 1876 [-13.91%]
TestClass.StructProperty 2640 2468 [-6.52%] 2458 [-6.89%] 2354 [-10.83%]
TestClass.StructField 2828 2917 [+3.15%] 2689 [-4.92%] 2852 [+0.85%]
TestClass.ClassProperty 2397 2110 [-11.97%] 1877 [-21.69%] 1780 [-25.74%]
TestClass.ClassField 2297 2104 [-8.4%] 1743 [-24.12%] 1755 [-23.6%]
TestStruct.IntProperty 1860 1587 [-14.68%] 1406 [-24.41%] 9108 [+389.68%]
TestStruct.IntField 1803 1523 [-15.53%] 1279 [-29.06%] 9346 [+418.36%]
TestStruct.StringProperty 2639 2139 [-18.95%] 1666 [-36.87%] 10022 [+279.77%]
TestStruct.StringField 2536 2224 [-12.3%] 1622 [-36.04%] 10014 [+294.87%]
TestStruct.StructProperty 2752 2689 [-2.29%] 1895 [-31.14%] 9910 [+260.1%]
TestStruct.StructField 3016 2905 [-3.68%] 2196 [-27.19%] 10409 [+245.13%]
TestStruct.ClassProperty 2587 2136 [-17.43%] 1657 [-35.95%] 10094 [+290.18%]
TestStruct.ClassField 2654 2208 [-16.8%] 1622 [-38.88%] 10055 [+278.86%]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment