The benchmark of mapping alternatives in .NET; 🚨🚨 * Manual Map * Reflection Map * AutoMapper Map The benchmark result shows that whenever you have an option to map manually ClassA to ClassB, go for a Manual Map. It is the most efficient way. If you want to make it automatic and cleaner, go for AutoMapper Map. If you want to make it automatic but do not have any third-party dependency, go for Reflection Map, but it is the worst case in terms of efficiency. #dotnet #dotnetcore #benchmark
Barış Can Tanrıverdi’s Post
More Relevant Posts
-
🚀 𝐖𝐡𝐲 𝐘𝐨𝐮 𝐒𝐡𝐨𝐮𝐥𝐝 𝐔𝐬𝐞 𝐈𝐄𝐪𝐮𝐚𝐭𝐚𝐛𝐥𝐞 𝐟𝐨𝐫 𝐑𝐞𝐟𝐞𝐫𝐞𝐧𝐜𝐞 𝐓𝐲𝐩𝐞𝐬 𝐢𝐧 𝐃𝐢𝐜𝐭𝐢𝐨𝐧𝐚𝐫𝐲 𝐊𝐞𝐲𝐬 𝐢𝐧 𝐂# 🔑 𝐖𝐇𝐘 𝐈𝐓 𝐌𝐀𝐓𝐓𝐄𝐑𝐒 If you're in a situation where you need to use reference types as dictionary keys, implementing 𝐈𝐄𝐪𝐮𝐚𝐭𝐚𝐛𝐥𝐞 is essential. Why? Because by default, 𝐰𝐡𝐞𝐧 𝐫𝐞𝐟𝐞𝐫𝐞𝐧𝐜𝐞 𝐭𝐲𝐩𝐞𝐬 𝐚𝐫𝐞 𝐮𝐬𝐞𝐝 𝐚𝐬 𝐤𝐞𝐲𝐬, 𝐝𝐢𝐜𝐭𝐢𝐨𝐧𝐚𝐫𝐢𝐞𝐬 𝐫𝐞𝐥𝐲 𝐨𝐧 𝐫𝐞𝐟𝐞𝐫𝐞𝐧𝐜𝐞 𝐞𝐪𝐮𝐚𝐥𝐢𝐭𝐲, 𝐰𝐡𝐢𝐜𝐡 𝐜𝐚𝐧 𝐥𝐞𝐚𝐝 𝐭𝐨 𝐮𝐧𝐞𝐱𝐩𝐞𝐜𝐭𝐞𝐝 𝐛𝐞𝐡𝐚𝐯𝐢𝐨𝐫. For example, imagine using Person objects as keys in a dictionary. 𝐖𝐢𝐭𝐡𝐨𝐮𝐭 𝐈𝐄𝐪𝐮𝐚𝐭𝐚𝐛𝐥𝐞, 𝐲𝐨𝐮 𝐜𝐨𝐮𝐥𝐝 𝐚𝐜𝐜𝐢𝐝𝐞𝐧𝐭𝐚𝐥𝐥𝐲 𝐚𝐝𝐝 𝐭𝐰𝐨 𝐏𝐞𝐫𝐬𝐨𝐧 𝐨𝐛𝐣𝐞𝐜𝐭𝐬 𝐰𝐢𝐭𝐡 𝐭𝐡𝐞 𝐬𝐚𝐦𝐞 𝐍𝐚𝐦𝐞 𝐩𝐫𝐨𝐩𝐞𝐫𝐭𝐲 (𝐰𝐡𝐢𝐜𝐡, 𝐢𝐧 𝐫𝐞𝐚𝐥𝐢𝐭𝐲, 𝐫𝐞𝐩𝐫𝐞𝐬𝐞𝐧𝐭 𝐭𝐡𝐞 𝐬𝐚𝐦𝐞 𝐩𝐞𝐫𝐬𝐨𝐧) 𝐦𝐮𝐥𝐭𝐢𝐩𝐥𝐞 𝐭𝐢𝐦𝐞𝐬. This can create data duplication and integrity issues. However, when you implement IEquatable and, for instance, 𝐞𝐧𝐬𝐮𝐫𝐞 𝐭𝐡𝐚𝐭 𝐞𝐪𝐮𝐚𝐥𝐢𝐭𝐲 𝐢𝐬 𝐛𝐚𝐬𝐞𝐝 𝐨𝐧 𝐭𝐡𝐞 𝐍𝐚𝐦𝐞 𝐩𝐫𝐨𝐩𝐞𝐫𝐭𝐲, 𝐭𝐡𝐞 𝐝𝐢𝐜𝐭𝐢𝐨𝐧𝐚𝐫𝐲 𝐰𝐢𝐥𝐥 𝐭𝐫𝐞𝐚𝐭 𝐭𝐰𝐨 𝐏𝐞𝐫𝐬𝐨𝐧 𝐨𝐛𝐣𝐞𝐜𝐭𝐬 𝐰𝐢𝐭𝐡 𝐭𝐡𝐞 𝐬𝐚𝐦𝐞 𝐍𝐚𝐦𝐞 𝐚𝐬 𝐞𝐪𝐮𝐚𝐥. This ensures that the same person can’t be added more than once, preserving data integrity. 💡 ✨ 𝐓𝐀𝐊𝐄𝐀𝐖𝐀𝐘 𝐖𝐡𝐞𝐧 𝐮𝐬𝐢𝐧𝐠 𝐫𝐞𝐟𝐞𝐫𝐞𝐧𝐜𝐞 𝐭𝐲𝐩𝐞𝐬 𝐚𝐬 𝐝𝐢𝐜𝐭𝐢𝐨𝐧𝐚𝐫𝐲 𝐤𝐞𝐲𝐬, 𝐢𝐭'𝐬 𝐚 𝐠𝐨𝐨𝐝 𝐩𝐫𝐚𝐜𝐭𝐢𝐜𝐞 𝐭𝐨 𝐢𝐦𝐩𝐥𝐞𝐦𝐞𝐧𝐭 𝐈𝐄𝐪𝐮𝐚𝐭𝐚𝐛𝐥𝐞 𝐭𝐨 𝐞𝐧𝐬𝐮𝐫𝐞 𝐩𝐫𝐨𝐩𝐞𝐫 𝐞𝐪𝐮𝐚𝐥𝐢𝐭𝐲 𝐜𝐡𝐞𝐜𝐤𝐬. This helps prevent duplicate entries and promotes consistent, reliable behavior in your application.
To view or add a comment, sign in
-
🚀 𝗛𝗼𝘄 𝘁𝗼 𝗖𝗼𝗺𝗯𝗶𝗻𝗲 𝗘𝗻𝘂𝗺 𝗩𝗮𝗹𝘂𝗲𝘀 𝗨𝘀𝗶𝗻𝗴 𝗙𝗹𝗮𝗴𝘀 𝗳𝗼𝗿 𝗙𝗹𝗲𝘅𝗶𝗯𝗶𝗹𝗶𝘁𝘆 🚀 📝 𝗕𝗮𝗰𝗸𝗴𝗿𝗼𝘂𝗻𝗱: Consider a case study where managing user roles and permissions in an application becomes complex due to various role combinations. Traditional enums with explicit checks can lead to rigid and cumbersome code, especially as new roles or permissions are introduced. ⚠️ 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲: If traditional enums were used to represent roles, it would require manual checks for each combination. This approach could result in a rigid and difficult-to-maintain codebase, especially as new roles are added. 💡 𝗦𝗼𝗹𝘂𝘁𝗶𝗼𝗻: Using [𝗙𝗹𝗮𝗴𝘀] enums offers a more flexible and efficient approach. 𝗛𝗲𝗿𝗲’𝘀 𝘄𝗵𝘆 𝘁𝗵𝗶𝘀 𝗺𝗲𝘁𝗵𝗼𝗱 𝗶𝘀 𝗮 𝗴𝗼𝗼𝗱 𝗮𝗹𝘁𝗲𝗿𝗻𝗮𝘁𝗶𝘃𝗲: 🧮 𝗣𝗼𝘄𝗲𝗿𝘀 𝗼𝗳 𝗧𝘄𝗼: Flags enums use powers of two (0, 1, 2, 4, 8, etc.) to enable efficient bitwise operations. Each value represents a single bit, allowing roles to be combined and checked easily. 𝗙𝗼𝗿 𝗶𝗻𝘀𝘁𝗮𝗻𝗰𝗲, 𝗰𝗼𝗺𝗯𝗶𝗻𝗶𝗻𝗴 𝗥𝗼𝗹𝗲.𝗠𝗼𝗱𝗲𝗿𝗮𝘁𝗼𝗿 (𝟰) 𝗮𝗻𝗱 𝗥𝗼𝗹𝗲.𝗔𝗱𝗺𝗶𝗻 (𝟴) 𝗿𝗲𝘀𝘂𝗹𝘁𝘀 𝗶𝗻 𝟭𝟮 (𝟭𝟭𝟬𝟬 𝗶𝗻 𝗯𝗶𝗻𝗮𝗿𝘆), 𝘀𝗵𝗼𝘄𝗶𝗻𝗴 𝗯𝗼𝘁𝗵 𝗿𝗼𝗹𝗲𝘀 𝗮𝗿𝗲 𝗮𝗰𝘁𝗶𝘃𝗲. - 𝚟𝚊𝚛 𝚌𝚘𝚖𝚋𝚒𝚗𝚎𝚍𝚁𝚘𝚕𝚎𝚜 = 𝚁𝚘𝚕𝚎.𝙼𝚘𝚍𝚎𝚛𝚊𝚝𝚘𝚛 | 𝚁𝚘𝚕𝚎.𝙰𝚍𝚖𝚒𝚗; - 📦 𝗘𝗳𝗳𝗶𝗰𝗶𝗲𝗻𝘁 𝗦𝘁𝗼𝗿𝗮𝗴𝗲: Flags enums can be stored as a single integer in a database, simplifying data management. This avoids the complexity associated with storing and checking lists of enums, making it easier to manage multiple roles. 🔍 𝗖𝗹𝗮𝗿𝗶𝘁𝘆 𝗮𝗻𝗱 𝗠𝗮𝗶𝗻𝘁𝗮𝗶𝗻𝗮𝗯𝗶𝗹𝗶𝘁𝘆: Using flags enums enhances code clarity and maintainability. Bitwise operations simplify the process of checking which roles are set, reducing the need for complex list manipulations. By using flags enums, the case study illustrates how roles and permissions can be managed with flexibility and efficiency. 𝗧𝗵𝗶𝘀 𝗮𝗽𝗽𝗿𝗼𝗮𝗰𝗵 𝗼𝗳𝗳𝗲𝗿𝘀 𝗮𝗻 𝗮𝗹𝘁𝗲𝗿𝗻𝗮𝘁𝗶𝘃𝗲 𝘁𝗼 𝘁𝗿𝗮𝗱𝗶𝘁𝗶𝗼𝗻𝗮𝗹 𝗲𝗻𝘂𝗺𝘀, 𝗽𝗼𝘁𝗲𝗻𝘁𝗶𝗮𝗹𝗹𝘆 𝘀𝗶𝗺𝗽𝗹𝗶𝗳𝘆𝗶𝗻𝗴 𝗰𝗼𝗱𝗲 𝗺𝗮𝗶𝗻𝘁𝗲𝗻𝗮𝗻𝗰𝗲 𝗮𝗻𝗱 𝗶𝗺𝗽𝗿𝗼𝘃𝗶𝗻𝗴 𝗰𝗹𝗮𝗿𝗶𝘁𝘆 𝗳𝗼𝗿 𝗺𝗮𝗻𝗮𝗴𝗶𝗻𝗴 𝗰𝗼𝗺𝗽𝗹𝗲𝘅 𝘀𝗰𝗲𝗻𝗮𝗿𝗶𝗼𝘀. #dotnet #softwaredevelopment
To view or add a comment, sign in
-
🚀 𝐁𝐞𝐧𝐜𝐡𝐦𝐚𝐫𝐤𝐢𝐧𝐠 .𝐍𝐄𝐓 𝐂𝐨𝐥𝐥𝐞𝐜𝐭𝐢𝐨𝐧𝐬: 𝐓𝐡𝐞 𝐇𝐢𝐝𝐝𝐞𝐧 𝐈𝐦𝐩𝐚𝐜𝐭 𝐨𝐟 𝐁𝐨𝐱𝐢𝐧𝐠 🚀 In the world of software development, understanding the performance of different data structures is crucial. I recently conducted a benchmark to compare the performance of 𝐋𝐢𝐬𝐭<𝐢𝐧𝐭>, 𝐢𝐧𝐭[], 𝐈𝐋𝐢𝐬𝐭<𝐢𝐧𝐭>, and 𝐈𝐄𝐧𝐮𝐦𝐞𝐫𝐚𝐛𝐥𝐞<𝐢𝐧𝐭> in .NET, with a focus on the impact of boxing during 𝐟𝐨𝐫𝐞𝐚𝐜𝐡 loops. The results are quite revealing! 🔍 𝐁𝐞𝐧𝐜𝐡𝐦𝐚𝐫𝐤 𝐒𝐞𝐭𝐮𝐩: • Methods: • SumArray • SumList • SumIList • SumIEnumerable • Data: • Collections containing 1000 integers. 📈 𝐊𝐞𝐲 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲𝐬: 1. Performance Impact: • 𝐀𝐫𝐫𝐚𝐲: The fastest, with an average time of ~256.7 ns. • 𝐋𝐢𝐬𝐭: Slightly slower than Array, with an average time of ~374.5 ns. • 𝐈𝐋𝐢𝐬𝐭 𝐚𝐧𝐝 𝐈𝐄𝐧𝐮𝐦𝐞𝐫𝐚𝐛𝐥𝐞: Significantly slower due to boxing, with times of ~1,774.4 ns and ~1,764.4 ns, respectively. 2. Boxing Overhead: • 𝐀𝐫𝐫𝐚𝐲 𝐚𝐧𝐝 𝐋𝐢𝐬𝐭: Avoid boxing as they have their own custom GetEnumerator implementations, which directly handle value types without causing boxing. • 𝐈𝐋𝐢𝐬𝐭 𝐚𝐧𝐝 𝐈𝐄𝐧𝐮𝐦𝐞𝐫𝐚𝐛𝐥𝐞: Cause boxing on each iteration due to their GetEnumerator method calls. This method involves converting value types, like 'int', to 'object', leading to performance degradation. 3. Memory Allocation: • 𝐀𝐫𝐫𝐚𝐲 𝐚𝐧𝐝 𝐋𝐢𝐬𝐭: Efficient, with no additional memory allocation. • 𝐈𝐋𝐢𝐬𝐭 𝐚𝐧𝐝 𝐈𝐄𝐧𝐮𝐦𝐞𝐫𝐚𝐛𝐥𝐞: Allocate additional memory due to boxing, with ~40 B allocated. 🔍 𝐂𝐨𝐧𝐜𝐥𝐮𝐬𝐢𝐨𝐧: For scenarios focused on optimizing performance and memory usage, 𝐀𝐫𝐫𝐚𝐲 and 𝐋𝐢𝐬𝐭 provide clear advantages over 𝐈𝐋𝐢𝐬𝐭 and 𝐈𝐄𝐧𝐮𝐦𝐞𝐫𝐚𝐛𝐥𝐞 due to the absence of boxing overhead 𝐞𝐬𝐩𝐞𝐜𝐢𝐚𝐥𝐥𝐲 𝐰𝐡𝐞𝐧 𝐝𝐞𝐚𝐥𝐢𝐧𝐠 𝐰𝐢𝐭𝐡 𝐯𝐚𝐥𝐮𝐞 𝐭𝐲𝐩𝐞𝐬. Understanding these performance characteristics can help you build more efficient software. #dotnet #performance #benchmark #softwaredevelopment #optimization
To view or add a comment, sign in
-
🚀 𝐁𝐞𝐧𝐜𝐡𝐦𝐚𝐫𝐤𝐢𝐧𝐠 𝐏𝐞𝐫𝐟𝐨𝐫𝐦𝐚𝐧𝐜𝐞: 𝐒𝐲𝐬𝐭𝐞𝐦.𝐓𝐞𝐱𝐭.𝐉𝐬𝐨𝐧 𝐯𝐬. 𝐍𝐞𝐰𝐭𝐨𝐧𝐬𝐨𝐟𝐭.𝐉𝐬𝐨𝐧 𝐢𝐧 .𝐍𝐄𝐓 𝟖 🚀 In the ever-evolving world of software development, performance optimization is key. I recently conducted a benchmark to compare the serialization performance of 𝐒𝐲𝐬𝐭𝐞𝐦.𝐓𝐞𝐱𝐭.𝐉𝐬𝐨𝐧 and 𝐍𝐞𝐰𝐭𝐨𝐧𝐬𝐨𝐟𝐭.𝐉𝐬𝐨𝐧 𝐯𝟏𝟑.𝟎.𝟑 in .NET 8. The results provide useful insights! 🔍 𝐁𝐞𝐧𝐜𝐡𝐦𝐚𝐫𝐤 𝐒𝐞𝐭𝐮𝐩: • Methods: • SystemTextJson_Object_Serialize • NewtonsoftJson_Object_Serialize • SystemTextJson_List_Serialize • NewtonsoftJson_List_Serialize • Data: • A single test object with properties Id, Name, IsActive, and CreatedDate. • A list of 50 test objects with similar properties. 🔍 𝐊𝐞𝐲 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲𝐬: 1. Performance Impact: • System.Text.Json: Shows remarkable performance, with serialization times nearly half of those observed with Newtonsoft.Json. • Single Object: ~125.4 ns • List of Objects: ~5,454.1 ns • Newtonsoft.Json: While reliable, it shows slower performance. • Single Object: ~252.2 ns • List of Objects: ~10,728.1 ns 2. Memory Allocation: • System.Text.Json: Much more memory-efficient. • Single Object: ~192 B • List of Objects: ~9000 B • Newtonsoft.Json: Allocates significantly more memory. • Single Object: ~1552 B • List of Objects: ~29976 B 3. Consistency Across Tests: • System.Text.Json consistently outperforms Newtonsoft.Json across different data sizes and types, highlighting its efficiency for high-performance applications. 🔍 𝐂𝐨𝐧𝐜𝐥𝐮𝐬𝐢𝐨𝐧: For scenarios focused on optimizing performance and memory usage in .NET 8, 𝐒𝐲𝐬𝐭𝐞𝐦.𝐓𝐞𝐱𝐭.𝐉𝐬𝐨𝐧 provides clear advantages over 𝐍𝐞𝐰𝐭𝐨𝐧𝐬𝐨𝐟𝐭.𝐉𝐬𝐨𝐧 𝐯𝟏𝟑.𝟎.𝟑. The significant reduction in both execution time and memory allocation makes it an ideal choice for performance-critical applications. 𝐇𝐨𝐰𝐞𝐯𝐞𝐫, 𝐍𝐞𝐰𝐭𝐨𝐧𝐬𝐨𝐟𝐭 𝐉𝐬𝐨𝐧 𝐫𝐞𝐦𝐚𝐢𝐧𝐬 𝐚 𝐬𝐭𝐫𝐨𝐧𝐠 𝐜𝐚𝐧𝐝𝐢𝐝𝐚𝐭𝐞 𝐟𝐨𝐫 𝐬𝐜𝐞𝐧𝐚𝐫𝐢𝐨𝐬 𝐫𝐞𝐪𝐮𝐢𝐫𝐢𝐧𝐠 𝐞𝐱𝐭𝐞𝐧𝐬𝐢𝐯𝐞 𝐟𝐞𝐚𝐭𝐮𝐫𝐞𝐬 𝐚𝐧𝐝 𝐜𝐮𝐬𝐭𝐨𝐦𝐢𝐳𝐚𝐭𝐢𝐨𝐧𝐬. 👀 Keep an eye out for .NET 9’s updates to System.Text.Json, which will bring new advancements in JSON serialization. Check out the documentation: https://lnkd.in/d52SYv_E #dotnet #performance #serialization #optimization #softwaredevelopment #benchmark
To view or add a comment, sign in
-
🚀 Benchmarking Performance: Exceptions vs. Result Pattern in C# 🚀 In the world of software development, performance is crucial. I ran a benchmark comparing the performance of handling business logic with exceptions versus using a result pattern in C#. The results are quite revealing! 🔍 Benchmark Setup: • Test Methods: • TestWithException: Uses exceptions for error handling. • TestWithResultPattern: Uses a result pattern to handle errors gracefully. • Inputs: Empty string and a sample text string. 📈 Key Takeaways: 1. Performance Impact: • Exceptions: Handling errors with exceptions is significantly slower, with an average execution time of ~12,492 ns (nanoseconds) for the empty input case. • Result Pattern: Using the result pattern is much faster, with an average execution time of ~1.930 ns for the empty input case. 2. Memory Allocation: • Exceptions: Higher memory allocation at ~256 B (bytes) due to exception handling overhead. • Result Pattern: More efficient with ~32 B allocation, demonstrating reduced overhead and improved performance. 3. Consistency Across Inputs: • The performance difference remains consistent across different input sizes, highlighting the efficiency of the result pattern. 🔍 Conclusion: For scenarios where performance is critical and error handling needs to be efficient, the result pattern outperforms traditional exception handling by a large margin. It minimizes execution time and reduces memory footprint, making it a good choice for performance-sensitive applications. #Performance #Benchmarking #CSharp #SoftwareDevelopment
To view or add a comment, sign in
-
🚀 Implementing Event Sourcing in .NET: A Guide with MartenDB 🚀 In the evolving software architecture world, Event Sourcing has emerged as an effective pattern to capture state changes as a series of events. Leveraging MartenDB within the .NET ecosystem can bring significant benefits, such as auditability, scalability, and enhanced business insights. 📝 Understanding Event Sourcing: Learn how Event Sourcing captures every state change as immutable, providing a reliable historical view and a robust audit trail. 🌟 Introduction to MartenDB: Explore how MartenDB, a .NET library for PostgreSQL, simplifies Event Sourcing implementation with its native support for event streams, projections, and aggregations. 📚 Setting Up MartenDB: Follow a step-by-step guide to configuring MartenDB in your .NET application, including daemon modes and projection lifecycles. 💡 Practical Implementation: Delve into a comprehensive example involving a Product entity to understand the practical application of MartenDB for Event Sourcing. Learn how to start streams, append events, fetch events, and project events to state. 📊 Sample Database Snapshots: Visualize the setup of MartenDB and event sourcing in action with snapshots of the events table, aggregate state, and stream details. 🔗 Read the full article: https://lnkd.in/dVZnnT8f Peace | Love | Code #dotnet #dotnetcore #eventsourcing #softwarearchitecture
To view or add a comment, sign in