language
Call Us: 1-800-497-0151

Blog

OneStream Business Rule Performance Tips: Multithreading For Loops in C# and VB.NET

  • , Architect

With today's multi-core processor servers, using concurrent programming can significantly boost the performance of your Business Rule Code. One common scenario where concurrency shines is looping through large datasets or performing repetitive tasks. By multithreading these loops, we can divide and conquer, processing chunks of data in parallel. In this post, we'll discuss the "how" and "why" of multithreading for loops in both C# and VB.NET business rules.

Why Use Multithreading?

  • 1. Performance: In applications that require extensive CPU-bound operations, like calculations in a loop, multithreading can exploit multiple cores of a CPU, executing processes concurrently and improving overall execution speed.
  • 2. Responsiveness: In UI applications, offloading time-consuming loops to a separate thread keeps the user interface responsive. This ensures the application remains user-friendly, even during heavy processing.
  • 3. Resource Utilization: Multithreading can optimize resource utilization. It ensures that while one thread is waiting for resources (e.g., I/O operations), another can continue executing, ensuring the CPU isn't idling.

How to Multithread For Loops?

.NET offers several tools for parallelism, but for the sake of simplicity, we’ll focus on the Parallel.For method provided by the System.Threading.Tasks namespace.

VB.NET Example:

Let's take a simple for loop that does some synchronous, one at a time, CPU-bound operation:

To multithread this loop using Parallel.For, the code becomes:

C# Example:

Things to Keep in Mind:

  • 1. Not Always Faster: Introducing parallelism adds overhead. For small loops or tasks, the overhead might negate the benefits, even slowing down the execution. Always profile/time your application to see if multithreading offers real benefits.
  • 2. Thread Safety: When multiple threads can access shared data simultaneously, race conditions can occur. Ensure that your operations within the loop are thread-safe. If needed, use locks, but be wary of potential deadlocks.
  • 3. Max Degree of Parallelism: By default, Parallel.For utilizes as many system cores as available. However, you might not want to utilize all cores, especially in a shared system. Control this by setting MaxDegreeOfParallelism:

Ordering: Parallel execution doesn't guarantee that the iterations will execute in order. If ordering is crucial, reconsider if parallelism is the right choice for your loop.

In conclusion, multithreading for loops in both C# and VB.NET can be a powerful tool to improve performance, especially in CPU-bound scenarios. However, like all tools, it should be used judiciously and appropriately, considering thread safety and the specific needs of the application. Try using a parallel for loop in your rule and note the change in performance.

Contact Us MindStream Analytics

Want to know more about OneStream Business Rules Performance Tips? Please complete the form below and we'll get back to you shortly.


Partner SpotLight

OneStream Diamond Partner

OneStream CPM

OneStream aligns to your business needs and changes more quickly and easily than any other product by offering one platform and one model for all financial CPM solutions. OneStream employs Guided Workflows, validations and flexible mapping to deliver data quality confidence for all collections and analysis while reducing risk throughout the entire auditable financial process.

OneStream Profile