using System.Threading;
using System.Runtime.InteropServices;
// Target a specific processor for the thread to run on
public class ThreadProcessor
{
    [DllImport("kernel32.dll")]
    static extern IntPtr GetCurrentThread();
    [DllImport("kernel32.dll")]
    static extern IntPtr SetThreadAffinityMask(IntPtr hThread, IntPtr dwThreadAffinityMask);
 
    public static void Usage()
    {
        int cpu = 0;
        ThreadProcessor tp = new ThreadProcessor();
        Console.WriteLine("Spike CPU 1");
        tp.SpikeCPU(cpu);
 
        if (tp._ex != null)
        {
            Console.WriteLine(tp._ex.Message);
        }
        else
        {
 
            if (Environment.ProcessorCount > 1)
            {
                while (++cpu < Environment.ProcessorCount)
                {
                    Thread.Sleep(1000);
                    Console.WriteLine("Spike CPU " + (cpu + 1).ToString());
                    tp.SpikeCPU(cpu);
 
                    if (tp._ex != null)
                    {
                        Console.WriteLine(tp._ex.Message);
                        break;
                    }
                }
 
            }
            else // Either a single CPU or hyperthreading not enabled in the OS or the BIOS.
            {
                Console.WriteLine("This PC does not have two processors available.");
            }
        }
 
    }
 
    private Thread _worker;
    private const int PiSignificantDigits = 750; // Adjust to your processor
 
    // Spike the CPU and waith for it to finish
    public void SpikeCPU(int targetCPU)
    {
 
        // Create a worker thread for the work.
        _worker = new Thread(DoBusyWork);
 
        // Background is set so not to not prevent the
        // mainprocess from terminating if someone closes it.
        _worker.IsBackground = true;
 
        _worker.Start((object)targetCPU);
        _worker.Join(); // Wait for it to be done.
    }
 
 
    public void DoBusyWork(object target)
    {
        try
        {
            int processor = (int)target;
            Thread tr = Thread.CurrentThread;
 
            if (Environment.ProcessorCount > 1)
            {
                SetThreadAffinityMask(GetCurrentThread(),
                    new IntPtr(1 << processor));
            }
 
            CalculatePI.Process(PiSignificantDigits);
        }
        catch (Exception ex)
        {
            _ex = ex;
        }
 
    }
 
    public Exception _ex = null;
 
 
}

public class CalculatePI
{
    /*
     * Computation of the n'th decimal digit of \pi with very little memory.
     * Written by Fabrice Bellard on January 8, 1997.
     *
     * We use a slightly modified version of the method described by Simon
     * Plouffe in "On the Computation of the n'th decimal digit of various
     * transcendental numbers" (November 1996). We have modified the algorithm
     * to get a running time of O(n^2) instead of O(n^3log(n)^3).
     *
     * This program uses mostly integer arithmetic. It may be slow on some
     * hardwares where integer multiplications and divisons must be done
     * by software. We have supposed that 'int' has a size of 32 bits. If
     * your compiler supports 'long long' integers of 64 bits, you may use
     * the integer version of 'mul_mod' (see HAS_LONG_LONG). 
     */
 
    // Call this static to use.
    public static string Process(int digits)
    {
        StringBuilder result = new StringBuilder();
 
        result.Append("3.");
 
        DateTime StartTime = DateTime.Now;
 
        if (digits > 0)
        {
 
            for (int i = 0; i < digits; i += 9)
            {
                String ds = CalculatePiDigits(i + 1);
                int digitCount = Math.Min(digits - i, 9);
 
                if (ds.Length < 9)
                    ds = string.Format("{0:D9}", int.Parse(ds));
 
                result.Append(ds.Substring(0, digitCount));
            }
        }
 
        return result.ToString();
    }
 
 
    private static int mul_mod(int a, int b, int m)
    {
        return (int)(((long)a * (long)b) % m);
    }
 
    /* return the inverse of x mod y */
    private static int inv_mod(int x, int y)
    {
        int q, u, v, a, c, t;
 
        u = x;
        v = y;
        c = 1;
        a = 0;
 
        do
        {
            q = v / u;
 
            t = c;
            c = a - q * c;
            a = t;
 
            t = u;
            u = v - q * u;
            v = t;
        } while (u != 0);
 
        a = a % y;
 
        if (a < 0)
        {
            a = y + a;
        }
 
        return a;
    }
 
    /* return (a^b) mod m */
    private static int pow_mod(int a, int b, int m)
    {
        int r, aa;
 
        r = 1;
        aa = a;
 
        while (true)
        {
            if ((b & 1) != 0)
            {
                r = mul_mod(r, aa, m);
            }
 
            b = b >> 1;
 
            if (b == 0)
            {
                break;
            }
 
            aa = mul_mod(aa, aa, m);
        }
 
        return r;
    }
 
    /* return true if n is prime */
    private static bool is_prime(int n)
    {
        if ((n % 2) == 0)
        {
            return false;
        }
 
        int r = (int)Math.Sqrt(n);
 
        for (int i = 3; i <= r; i += 2)
        {
            if ((n % i) == 0)
            {
                return false;
            }
        }
 
        return true;
    }
 
    /* return the prime number immediatly after n */
    private static int next_prime(int n)
    {
        do
        {
            n++;
        } while (!is_prime(n));
 
        return n;
    }
 
    private static String CalculatePiDigits(int n)
    {
        int av, vmax, num, den, s, t;
 
        int N = (int)((n + 20) * Math.Log(10) / Math.Log(2));
 
        double sum = 0;
 
        for (int a = 3; a <= (2 * N); a = next_prime(a))
        {
            vmax = (int)(Math.Log(2 * N) / Math.Log(a));
 
            av = 1;
 
            for (int i = 0; i < vmax; i++)
            {
                av = av * a;
            }
 
            s = 0;
            num = 1;
            den = 1;
            int v = 0;
            int kq = 1;
            int kq2 = 1;
 
            for (int k = 1; k <= N; k++)
            {
 
                t = k;
 
                if (kq >= a)
                {
                    do
                    {
                        t = t / a;
                        v--;
                    } while ((t % a) == 0);
 
                    kq = 0;
                }
                kq++;
                num = mul_mod(num, t, av);
 
                t = 2 * k - 1;
 
                if (kq2 >= a)
                {
                    if (kq2 == a)
                    {
                        do
                        {
                            t = t / a;
                            v++;
                        } while ((t % a) == 0);
                    }
                    kq2 -= a;
                }
                den = mul_mod(den, t, av);
                kq2 += 2;
 
                if (v > 0)
                {
                    t = inv_mod(den, av);
                    t = mul_mod(t, num, av);
                    t = mul_mod(t, k, av);
 
                    for (int i = v; i < vmax; i++)
                    {
                        t = mul_mod(t, a, av);
                    }
 
                    s += t;
 
                    if (s >= av)
                    {
                        s -= av;
                    }
                }
 
            }
 
            t = pow_mod(10, n - 1, av);
            s = mul_mod(s, t, av);
            sum = (sum + (double)s / (double)av) % 1.0;
        }
 
        int Result = (int)(sum * 1e9);
 
        String StringResult = String.Format("{0:D9}", Result);
 
        return StringResult;
    }
 
    // Put a space between every group of 10 digits.
 
    private static String breakDigitsIntoGroupsOf10(String digits)
    {
        String result = "";
 
        while (digits.Length > 10)
        {
            result += digits.Substring(0, 10) + " ";
            digits = digits.Substring(10, digits.Length - 10);
        }
 
        result += digits;
 
        return result;
    }
 
}

Spike Your CPU’s Processor in .Net的更多相关文章

  1. [转]如何根据cpu的processor数来确定程序的并发线程数量

    原文:http://blog.csdn.net/kirayuan/article/details/6321967 我们可以在cat 里面发现processor数量,这里的processor可以理解为逻 ...

  2. [Windows内核分析]KPCR结构体介绍 (CPU控制区 Processor Control Region)

    Windows内核分析索引目录:https://www.cnblogs.com/onetrainee/p/11675224.html 逆向分析操作系统内核代码至少需要具备两项技能: 段页汇编代码非常懂 ...

  3. (转)SQL Server 性能调优(cpu)

    摘自:http://www.cnblogs.com/Amaranthus/archive/2012/03/07/2383551.html 研究cpu压力工具 perfom SQL跟踪 性能视图 cpu ...

  4. Linux学习总结(十四)—— 查看CPU信息

    文章首发于[博客园-陈树义],点击跳转到原文Linux学习总结(十四)-- 查看CPU信息. Linux学习总结(十四)-- 查看CPU信息 商用服务器CPU最常用的是 Intel Xeon 系列,该 ...

  5. Windows 性能监视器的基本指标说明(CPU,内存,硬盘参数)

    [转]Windows 性能监视器的基本指标说明(CPU,内存,硬盘参数) 作为一个系统工程师来说,要看懂监控的数据至关重要,关系着优化和分析出现的问题.我是在运维过程中要用到的.因此,今天给出Wind ...

  6. linux 查看 cpu个数 核心数 线程数

    深蓝的blog:http://blog.csdn.net/huangyanlong/article/details/43935535 (1).查看cpu信息 [root@xckydb ~]# cat ...

  7. Linux查看cpu个数

    [root@lidongbo~]# cat /proc/cpuinfo processor       : 0 vendor_id       : GenuineIntel cpu family    ...

  8. [转]检测SQLSERVER数据库CPU瓶颈及内存瓶颈

    在任务管理器中看到sql server 2000进程的内存占用,而在sql server 2005中,不能在任务管理器中查看sql server 2005进程的内存占用,要用 以下语句查看sql se ...

  9. [转]SQL Server 性能调优(cpu)

      研究cpu压力工具 perfom SQL跟踪 性能视图 cpu相关的wait event Signal wait time SOS_SCHEDULER_YIELD等待 CXPACKET等待 CME ...

随机推荐

  1. mac上如何搜索文件?

    在Mac上如果你用会了搜索功能那绝对是个事半功倍的技巧.因为Mac本身有强大的文件索引能力, 可以帮你快速的找到你需要的文件.就好比我要找到上周修改过的word文档应该怎么办? * 使用语音命令让Si ...

  2. SQLite和MySQL数据库的差别与应用

    简单来说,SQLITE功能简约.小型化,追求最大磁盘效率:MYSQL功能全面,综合化.追求最大并发效率.假设仅仅是单机上用的,数据量不是非常大.须要方便移植或者须要频繁读/写磁盘文件的话.就用SQLi ...

  3. php strlen()函数 语法

    php strlen()函数 语法 作用:返回字符串的长度.大理石平台价格 语法:strlen(string) 参数: 参数 描述 string 必需.规定要检查的字符串.     说明:返回字符串的 ...

  4. PHP-利用二叉堆实现TopK-算法

    介绍 在以往工作或者面试的时候常会碰到一个问题,如何实现海量TopN,就是在一个非常大的结果集里面快速找到最大的前10或前100个数,同时要保证内存和速度的效率,我们可能第一个想法就是利用排序,然后截 ...

  5. bzoj 2002 Bounce 弹飞绵羊(分块)

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 11202  Solved: 5698[Subm ...

  6. Gym 100917M Matrix, The

    题目链接: http://codeforces.com/gym/100917/problem/M --------------------------------------------------- ...

  7. Android O编译前修改文件和目录权限

    当需要修改某文件或路径权限时,我们可以在init.rc开机启动某节点添加chmod命令进行修改.但是对于system分区,由于是ro权限,在init.rc使用chmod修改权限无效.需要在文件编译时, ...

  8. CentOS7 - 安装 VirtualBox

    参考资料 最新的可用安装包可以从这里下载 VirtualBox 是 x86 硬件虚拟化产品,功能上与 VMware Server.KVM.及 Xen 类似,但是 VirtualBox 不修改 Linu ...

  9. HBase备份还原OpenTSDB数据之Export/Import(增量+全量)

    前言 本文基于伪分布式搭建 hadoop+zookeeper+hbase+opentsdb之后,文章链接:https://www.cnblogs.com/yybrhr/p/11128149.html, ...

  10. Android深度探索-卷1第八章心得体会

    本章介绍了如何将Linux驱动分成多个实现文件和Linux常用的代码重用方式还有些强行卸载Linux驱动的方法 开发一个Linux驱动,可能会在init.exit等函数中发生错误导致Linux驱动安装 ...