Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I need a slow C# function

For some testing I'm doing I need a C# function that takes around 10 seconds to execute. It will be called from an ASPX page, but I need the function to eat up CPU time on the server, not rendering time. A slow query into the Northwinds database would work, or some very slow calculations. Any ideas?

like image 583
Sisiutl Avatar asked Oct 21 '12 19:10

Sisiutl


People also ask

What is the point of a slow cooker?

As a result of the long, low-temperature cooking, slow cookers help tenderize less-expensive cuts of meat. A slow cooker brings out the flavor in foods. A wide variety of foods can be cooked in a slow cooker, including one pot meals, soups, stews and casseroles. A slow cooker uses less electricity than an oven.

What can you use instead of a slow cooker?

As for what kind of pot to use, most commenters agreed that a heavy casserole dish or Dutch oven would do the job nicely. One person particularly recommended using a cast-iron Dutch oven for its ability to evenly distribute heat and for the fact that it supposedly imparts a better flavor on the food.

What is the smallest size slow cooker?

Bella - 1.5-qt. "Smallest slow cooker...

How much does a slow cooker cost?

Slow cookers cost around $30 to $100 to buy and can save you hundreds over purchasing fancy countertop convection toaster ovens and broilers. Slow cookers can also cut your grocery bill significantly by allowing you to buy cheaper cuts of meat and tenderizing them over low heat for a longer time.


2 Answers

Try to calculate nth prime number to simulate CPU intensive work -

public void Slow() {     long nthPrime = FindPrimeNumber(1000); //set higher value for more time }  public long FindPrimeNumber(int n) {     int count=0;     long a = 2;     while(count<n)     {         long b = 2;         int prime = 1;// to check if found a prime         while(b * b <= a)         {             if(a % b == 0)             {                 prime = 0;                 break;             }             b++;         }         if(prime > 0)         {             count++;         }         a++;     }     return (--a); } 

How much time it will take will depend on the hardware configuration of the system.

So try with input as 1000 then either increase input value or decrease it.

This function will simulate CPU intensive work.

like image 52
Parag Meshram Avatar answered Sep 22 '22 06:09

Parag Meshram


Arguably the simplest such function is this:

public void Slow() {     var end = DateTime.Now + TimeSpan.FromSeconds(10);     while (DateTime.Now < end)            /* nothing here */ ; } 
like image 21
Motti Avatar answered Sep 21 '22 06:09

Motti