An application that loads a huge volume of data at once will have performance issues like high time consumption and expensive computations. To optimize these workloads and effectively process data, we can use CPU-bound and I/O-bound operations in our app.
In this blog, we will see the advantages of using CPU-bound operations and how to implement them in an ASP.NET Core app.
Synchronous and asynchronous operations
First, let’s see the differences between synchronous and asynchronous operations.
A synchronous operation follows the specified steps of an algorithm where it waits for the previous action to complete to start the next operation. Whereas an asynchronous operation executes each action independently, and the result of the previous action doesn’t affect the flow of the operation.
In other words, the asynchronous operation allows the operations to run parallel in multiple threads to complete a task. We can use asynchronous operations to fetch data from huge databases and other time-consuming actions.
You can identify an asynchronous operation with the help of the keywords async and await associated with them.
The await operations can be split into:
I/O-bound operations
An I/O-bound await operation denotes the rate at which an operation is executed by the speed of the I/O system. You can await an operation that returns a Task or Task<T> using the async method.
The I/O-bound operation is very time-consuming, as the operating system (OS) has to manage the workload. Since each action is performed synchronously, it affects the UI and also affects the performance.
CPU-bound operations
A CPU-bound operation denotes the rate at which an operation is executed by the speed of the CPU. In other words, the CPU takes control of all the processes. You can await an operation that is started in the background with the thread Task.Run method.
The CPU-bound operation makes use of all the available processors and executes parallel operations through multithreading instead of running it in the main thread. It executes operations in the background without affecting the UI.
Advantages of CPU-bound operations
Advantages of using CPU-bound operations over I/O-bound operations:
- Increases the performance of the processor.
- Provides better efficiency in scheduling and multithreading processes.
Example of CPU-bound operations
Let’s create an ASP.NET Core app and learn how to implement CPU-bound operations in it.
Step 1: Create an example ASP.NET Core project.
First, create an ASP.NET Core project. To do so, open Visual Studio. Then, click on Create a new project.
In the Create a new project dialog, select the ASP.NET Core Empty option from the list and click Next.
Refer to the following image.
Step 2: Add namespaces.
After creating the project, add the following namespaces to the project.
using System; using System.IO; using System.Threading.Tasks;
Step 3: Implement CPU-bound operations.
Let’s see how to return an employee’s salary using CPU-bound operations.
namespace Cpu_bound_sample { class Program { static void Main(string [] args) { Salary(213434); Discount(); } private static void Discount() { Console.WriteLine("Started some synchronous task"); } private static async void Salary(float value) { Console.WriteLine("Started CPU Bound asynchronous task on a background thread"); //CPU bound operation is called here; await Task.Run(() => SavingText(value)); } //This method runs in the background; private static async void SavingText(float value { //Finds the location path string path = @"D:\Cpu.txt"; try { if (!File.Exists(path)) { using (var myFile = File.Create(path)) { //Writes the file: TextWriter tw = new StreamWriter(path, false); tw.WriteLine(value); tw.Close(); } } else if (File.Exists(path)) { using (var myFile = File.Create(path)) { //writes the file: TextWriter tw = new StreamWriter(path, true); tw.WriteLine(value); tw.Close(); } } } catch (Exception ex) { } }
You’ll find the CPU-bound operation is called in the Salary() method, which allows this SavingText method (task) to run in the background. Without taking any instance of time, the SavingText method will be performed in parallel by using multiple threads. So, the required task is completed within a limited time.
We will get output like in the following screenshot.
GitHub reference
Find the complete example at Perform CPU-bound operations in ASP.NET Core on GitHub.
Conclusion
Thanks for reading! The most efficient way to run an asynchronous task is achieved by handling it with a CPU-bound operation. Mostly, a CPU-bound operation can be scaled separately so it does not affect the rest of an app. So, try out the steps given in this blog and enjoy better productivity!
The Syncfusion ASP.NET Core UI controls library, powered by Essential JS 2, is the only suite you will ever need to build an app. It contains over 70 high-performance, lightweight, modular, and responsive UI controls in a single package. Use them to build stunning web apps!
If you’re already a Syncfusion user, you can download the product setup. If not, you can download a free 30-day trial to evaluate our products.
You can contact us through our support forum, support portal, or feedback portal. As always, we are happy to assist you!