English 中文(简体)
计算最低/最高/平均费用
原标题:Calculating minimum/maximum/average ping reponse

因此,这里的是一种应用,它测试了每个广受欢迎的RoneScape游戏服务器。 我正在每台139台服务器上运行,并增加阵列的滞后价值。 在通过清单条目登记时,可以计算每个服务器的平均、最低和最高滞后情况。

现在,在我的代码中,当一个不可击退的服务器被注意到的最低滞后显示为0。 我不敢确定,如果我的平时反应达到排出时间、排数为零。

我可以做些什么来避免在服务器倒塌时我的最低点。

using System;
using System.Net.NetworkInformation;
using System.Collections.Generic;

namespace RuneScape_Ping_Tool
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write(Environment.NewLine + "Start the test? (y/n): ");

            if (Console.Read() == char.Parse("y"))
            {
                Console.WriteLine();
                Ping();
            }
        }

        static void Ping()
        {
            List<int> lag = new List<int>();

            for (int server = 1; server <= 139; server++)
            {
                string url = "world" + server.ToString() + ".runescape.com";

                Console.WriteLine("Checking world " + server + "...");

                Ping ping = new Ping();
                PingReply reply = ping.Send(url);

                lag.Add(int.Parse(reply.RoundtripTime.ToString()));
            }

            for (int i = 1; i <= 139; i++)
            {
                Console.WriteLine("World " + i + ": " + lag[i - 1]);
            }

            int average = 0;
            int highest = 1;
            int lowest = 1000;

            int highestWorld = 0;
            int lowestWorld = 0;

            for (int i = 1; i <= 139; i++)
            {
                average = average + lag[i - 1];
            }

            for (int i = 1; i <= 139; i++)
            {
                if (highest < lag[i - 1])
                {
                    highest = lag[i - 1];
                    highestWorld = i;
                }
            }

            for (int i = 1; i <= 139; i++)
            {
                if (lowest > lag[i - 1])
                {
                    lowest = lag[i - 1];
                    lowestWorld = i;
                }
            }

            Console.WriteLine();
            Console.WriteLine("Average lag: " + average / 139);
            Console.WriteLine("Highest lag: " + highest + " in world " + highestWorld);
            Console.WriteLine("Lowest lag: " + lowest + " in world " + lowestWorld);

            Console.Write(Environment.NewLine + "Start the test? (y/n): ");

            if (Console.Read() == char.Parse("y"))
            {
                Console.WriteLine();
                Ping();
            }
        }
    }
}
最佳回答

你们能否ski过零? 而且,你也可以把所有计算方法放在一个单一循环中。

int n = 0 // number of data points

for (int i = 0; i < 139; ++i) {
  if (lag[i] == 0) {
    continue;  // skip 0 values
  }
  ++n;
  sum += lag[i];

  if (highest < lag[i]) {
    highest = lag[i];
    highestWorld = i + 1;
  }

  if (lowest > lag[i]) {
    lowest = lag[i];
    lowestWorld = i + 1;
  }

  average = sum / n;  // Note: you may want to round this.
}
问题回答

首先考虑同时处理所有事项(当时有4个):

var servers = Enumerable.Range(1, 139).Select(i => String.Format("world{0}.runescape.com",i));
var results = servers.AsParallel()
                     .WithDegreeOfParallelism(4)
                     .Select(server => new Ping().Send(server))
                     .ToList();

然后只收集有效结果,使用<代码>PingReply.Status的说明,而不是查询0:

var validResults = results.Where(r => r.Status == IPStatus.Success)
                          .Select(r => r.RoundtripTime);

在这方面,你需要:

Console.WriteLine("Total Results: {0}", results.Count());
Console.WriteLine("Valid Results: {0}", validResults.Count());
Console.WriteLine("Min from Valid: {0}", validResults.Min());
Console.WriteLine("Max from Valid: {0}", validResults.Max());
Console.WriteLine("Avg from Valid: {0}", validResults.Average());

你们能够以这些职能获得敏锐、最大和平均。

var nozeros = lag.Where(i => i > 0);
int lowest = nozeros.Min();
int lowestWorld = lag.IndexOf(lowest);
int highest = nozeros.Max();
int highestWorld = lag.IndexOf(highest);
int average = (int)nozeros.Average();




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签