output() public static method

public static output ( string name, double pi, long iterations, double elapseTime, int threadCount ) : void
name string
pi double
iterations long
elapseTime double
threadCount int
return void
Exemplo n.º 1
0
    private static void execute(int numberOfTasks)
    {
        const int    n     = 1000000000;
        const double delta = 1.0 / n;
        long         startTimeHundredsOfNanos = System.DateTime.Now.Ticks;
        int          sliceSize = n / numberOfTasks;

        System.Threading.Thread[] threads = new System.Threading.Thread[numberOfTasks];
        Accumulator accumulator           = new Accumulator();

        for (int i = 0; i < numberOfTasks; ++i)
        {
            Task task = new Task(1 + i * sliceSize, (i + 1) * sliceSize, delta, accumulator);
            threads[i] = new System.Threading.Thread(new System.Threading.ThreadStart(task.execute));
        }
        foreach (System.Threading.Thread t in threads)
        {
            t.Start();
        }
        foreach (System.Threading.Thread t in threads)
        {
            t.Join();
        }
        double pi         = 4.0 * delta * accumulator.getSum();
        double elapseTime = (System.DateTime.Now.Ticks - startTimeHundredsOfNanos) / 1e7;

        Output.output("Threads Task Class Delegate Sync Object", pi, n, elapseTime, numberOfTasks);
    }
Exemplo n.º 2
0
    public static void Main(string[] args)
    {
        const int    n     = 1000000000;
        const double delta = 1.0 / n;
        long         startTimeHundredsOfNanos = System.DateTime.Now.Ticks;
        double       sum = 0.0;

        for (int i = 1; i <= n; ++i)
        {
            double x = (i - 0.5) * delta;
            sum += 1.0 / (1.0 + x * x);
        }
        double pi         = 4.0 * delta * sum;
        double elapseTime = (System.DateTime.Now.Ticks - startTimeHundredsOfNanos) / 1e7;

        Output.output("Sequential", pi, n, elapseTime);
    }
    private static void execute(int numberOfTasks)
    {
        const int    n     = 1000000000;
        const double delta = 1.0 / n;
        long         startTimeHundredsOfNanos = System.DateTime.Now.Ticks;
        int          sliceSize = n / numberOfTasks;

        System.Threading.Thread[] threads = new System.Threading.Thread[numberOfTasks];
        Accumulator accumulator           = new Accumulator();

        for (int i = 0; i < numberOfTasks; ++i)
        {
            int start = 1 + i * sliceSize;
            int end   = (i + 1) * sliceSize;
            threads[i] = new System.Threading.Thread(new System.Threading.ThreadStart(delegate() {
                double localSum = 0.0;
                for (int j = start; j <= end; ++j)
                {
                    double x  = (j - 0.5) * delta;
                    localSum += 1.0 / (1.0 + x * x);
                }
                accumulator.add(localSum);
            }
                                                                                      ));
        }
        foreach (System.Threading.Thread t in threads)
        {
            t.Start();
        }
        foreach (System.Threading.Thread t in threads)
        {
            t.Join();
        }
        double pi         = 4.0 * delta * accumulator.getSum();
        double elapseTime = (System.DateTime.Now.Ticks - startTimeHundredsOfNanos) / 1e7;

        Output.output("Threads Anonymous Delegate Sync Object", pi, n, elapseTime, numberOfTasks);
    }