我需要一个缓慢的C#功能 [英] I need a slow C# function

查看:169
本文介绍了我需要一个缓慢的C#功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一些测试,我做我需要一个C#函数,采用10秒左右来执行。它将从ASPX页面调用,但我需要的功能,吃起来的CPU时间在服务器上,而不是渲染时间。慢查询到罗斯文数据库会的工作,或者一些很慢的计算。 ?任何想法

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?

推荐答案

试算第n个质数来模拟CPU密集型的工作 -

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.

因此,与输入尝试为1000,那么要么提高输入值或减少它。

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

这功能将模拟CPU紧张的工作。

This function will simulate CPU intensive work.

这篇关于我需要一个缓慢的C#功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆