Tôi đang sử dụng RedGate để thực hiện một số đánh giá hiệu suất. Tôi nhận thấy tự động tạo một cá thể bằng cách sử dụng Activator.CreateInstance
(với hai tham số hàm tạo) đang chiếm một khoảng thời gian khá ... có một phương án tốt hơn vẫn sử dụng một cách tiếp cận phản chiếu (không rõ ràng)?Activator.Create Performance Performance Alternative
Trả lời
Sử dụng lambda đã biên dịch nếu có thể, MUCH của nó nhanh hơn.
+1. Nếu điều đó vẫn chưa đủ nhanh thì bạn có thể thử phản xạ phát ra, nhưng điều đó không có khả năng mang lại nhiều cải tiến hơn một lambda biên dịch, và nó là một LOT phức tạp hơn để viết mã. –
Dịch vụ Biên dịch() có hoạt động chậm hơn hay nhanh hơn không? Tôi giả sử hiệu suất được tính mà không bao gồm thời gian biên dịch? Trong các tình huống trong đó nhiều phiên bản của các kiểu khác nhau là cần thiết và loại đối tượng được xác định trong thời gian chạy, sẽ chậm hơn rất nhiều khi có thao tác biên dịch mọi lúc hơn Activator.CreateInstance? –
Liên kết đó hiện là 404 –
Đừng quên về DynamicMethod
Dưới đây là ví dụ làm thế nào để tạo ra ví dụ mới qua constructor mặc định
public static ObjectActivator CreateCtor(Type type)
{
if (type == null)
{
throw new NullReferenceException("type");
}
ConstructorInfo emptyConstructor = type.GetConstructor(Type.EmptyTypes);
var dynamicMethod = new DynamicMethod("CreateInstance", type, Type.EmptyTypes, true);
ILGenerator ilGenerator = dynamicMethod.GetILGenerator();
ilGenerator.Emit(OpCodes.Nop);
ilGenerator.Emit(OpCodes.Newobj, emptyConstructor);
ilGenerator.Emit(OpCodes.Ret);
return (ObjectActivator)dynamicMethod.CreateDelegate(typeof(ObjectActivator));
}
public delegate object ObjectActivator();
đây là thêm về performance comparison
Đo InvokeMember .. 1000000 lần lặp trong 1,5643784 giây.
Đo MethodInfo.Invoke ... 1000000 lần lặp trong 0.8150111 giây.
Đo DynamicMethod ... 1000000 lần lặp trong 0,0330202 giây.
Đo cuộc gọi trực tiếp ... 1000000 lần lặp trong 0,0136752 giây.
so sánh hiệu suất [http://www.wintellect.com/blogs/krome/getting-to-know-dynamicmethod] hiển thị 404 ngay bây giờ. – Lcng
- 1. NLTK Performance
- 2. Generics vs Object performance
- 3. ReaderWriterLockSlim Extension Method Performance
- 4. linq foreach performance
- 5. JQuery Tìm Performance
- 6. Neo4J Performance Benchmarking
- 7. knockout observableArray performance
- 8. RowVersion and Performance
- 9. ConfigurationManager.AppSettings Concerns Performance
- 10. SimpleXML vs DOMDocument performance
- 11. Performance Monitoring: hạch vs Graphite
- 12. Racket Interactive vs Compiled Performance
- 13. PHP 5 Reflection API performance
- 14. Neo4j cypher query slow performance
- 15. Indexing Performance bigint vs VARCHAR
- 16. http HEAD with GET performance
- 17. Performance text/html vs application/json
- 18. Android Performance - 'Tránh nội Getters/setters'
- 19. Performance Benchmark CouchDB x Relational Databases
- 20. Interpreting Multicore Performance Trace (Eclipse/Android)
- 21. công cụ Performance Monitoring cho trình duyệt
- 22. F # Performance Profiler Instrumentation Không hoạt động
- 23. LINQ to Sql vs Entity Framework Performance
- 24. ASP.NET/SQL 2008 Performance Vấn đề
- 25. Hibernate Performance Thực hành tốt nhất?
- 26. Performance std :: strstr vs std :: string :: tìm
- 27. Performance quan tâm: StringCollection vs Danh sách <String>
- 28. mệnh đề OrderBy trước mệnh đề where - performance?
- 29. riêng WinForms kiểm soát thấp thoáng và Có Bad Performance
- 30. Performance vấn đề trên tùy chỉnh phông chữ TextView
Tôi đã cung cấp câu trả lời [ở đây] (http://stackoverflow.com/a/16162809/661933) cho trường hợp mặc định. – nawfal