线程在c#中的运行速度比java慢

时间:2016-03-16 15:51:49

标签: java c# multithreading parallel-processing java-threads

我的朋友和我决定用Java和C#编写类似的程序,使用线程将数据发布到网站。他的Java应用程序运行大约6秒,而用C#编写的代码需要23秒。看看我的C#代码,它运行缓慢是有原因的。我的线程是否导致错误共享,或者C#VM是否比Java更慢?

我们正在使用4个线程比较我们的代码。此外,我们在本地服务器上运行此服务器,因此服务器响应时间不是问题。

C#代码:

主要

Thread[] childThread = new Thread[4];
int i, j = 0, skip = 125;

for (i = 0; i < 500; i+=skip, j++) {
    threader threader = new threader(tokens.GetRange(i, skip));
    List<String> test = tokens.GetRange (i, skip);
    int size = test.Count;

    childref = new ThreadStart(() => threader.CallToChildThread());
    childThread[j] = new Thread(childref);
    childThread[j].Start();
}

穿线

public class threader {

    public List<String> tokens;

    public threader(List<String> tokens) {
        this.tokens = tokens;
    }

    public void CallToChildThread() {
        var watch = System.Diagnostics.Stopwatch.StartNew();

        foreach (String item in tokens) {
            using (var client = new WebClient()) {
                var values = new NameValueCollection();
                values["username"] = "blah";
                values["password"] = item;

                DateTime time = DateTime.Now;

                var response = client.UploadValues("http://example.com", values);
                if (!isFound) {
                    var responseString = Encoding.Default.GetString(response);

                    Console.WriteLine(item + " is a " + responseString);

                    if (responseString.Equals("valid")) {
                        isFound = true;

                        Console.WriteLine('\n');
                        Console.WriteLine(item + " is a " + responseString);
                        Console.WriteLine("===================");

                        watch.Stop();
                        var elapsedMs = watch.ElapsedMilliseconds;

                        Console.WriteLine(elapsedMs / 1000 + " Seconds");

                        return;
                    }
                } else {
                    return;
                }
            }
        }
    }
}

我的朋友在6秒内运行的Java代码是:

穿线

public class Threader extends Thread{
    public List<String> list;

    public Threader(List<String> list){
        this.list = new ArrayList<String>(list);
    }

    @Override
    public void run() {
        try {
            for (String  item : list) {
                String url = "http://example.com";
                URL obj = new URL(url);
                HttpURLConnection con = (HttpURLConnection) obj.openConnection();

                //add reuqest header
                con.setRequestMethod("POST");

                String urlParameters = "username=blah&password="+ item;

                // Send post request
                con.setDoOutput(true);
                DataOutputStream wr = new DataOutputStream(con.getOutputStream());
                wr.writeBytes(urlParameters);
                wr.flush();
                wr.close();

                int responseCode = con.getResponseCode();

                BufferedReader in = new BufferedReader(
                        new InputStreamReader(con.getInputStream()));
                String inputLine;
                StringBuffer response = new StringBuffer();

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();

                //print result
                if (!response.toString().equals("invalid")) {
                    System.out.println("Found it!");
                    System.exit(0);
                }
                System.out.println(response.toString() + " for " + item);
            }
        } catch (Exception e){e.printStackTrace();}
    }
}

0 个答案:

没有答案