Go語言和ASP.NET的一般處理常式在處理WEB請求時的速度比較

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

Go語言和ASP.NET的一般處理常式在處理WEB請求時的速度比較

1、首先寫一個Go語言的簡單WEB程式,就返回一個HelloWord!

package mainimport (    f "fmt"    "log"    "net/http"    //    "strings")func sayhelloName(w http.ResponseWriter, r *http.Request) {    //    r.ParseForm()    //    f.Println(r.Form)    //    f.Println("path", r.URL.Path)    //    f.Println("scheme", r.URL.Scheme)    //    f.Println(r.Form["url_long"])    //    for k, v := range r.Form {    //        f.Println("key:", k, "val:", strings.Join(v, "、"))    //    }    f.Fprintln(w, "hello world!")}func main() {    http.HandleFunc("/", sayhelloName)    err := http.ListenAndServe(":8080", nil)    if err != nil {        log.Fatal("ListenAndServe:", err)    }}

 

 

2、建一個Asp.net的Handler1.ashx的一般處理常式,並運行於IIS中。

using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace TESTWeb{    /// <summary>    /// Handler1 的摘要說明    /// </summary>    public class Handler1 : IHttpHandler    {        public void ProcessRequest(HttpContext context)        {            context.Response.Write("hello world!");        }        public bool IsReusable        {            get            {                return false;            }        }    }}

 

 

3、用C#寫一個控制台程式,用來類比發起10000次請求時,速度比較:

using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Text;namespace TESTSpeed{    class Program    {        static void Main(string[] args)        {                  WebClient wc = new WebClient();            DateTime dt1 = DateTime.Now;            for (int i = 0; i < 10000; i++)            {                var result = wc.DownloadString("http://localhost:48563/handler1.ashx?t=" + DateTime.Now.Ticks);            }            DateTime dt2 = DateTime.Now;            Console.WriteLine("Asp.net一般處理常式用時:" + (dt2 - dt1).TotalMilliseconds);            WebClient wc2 = new WebClient();            DateTime dt3 = DateTime.Now;            for (int i = 0; i < 10000; i++)            {                var result = wc2.DownloadString("http://127.0.0.1:8080/?t=" + DateTime.Now.Ticks);            }            DateTime dt4 = DateTime.Now;            Console.WriteLine("Go語言用時:" + (dt4 - dt3).TotalMilliseconds);        }    }}

 

4、測試結果:

Go語言用時:811.0464ms

Asp.net一般處理常式:14017.8017ms

5、結論:Go不是一般的快!!!!相差了17倍!!!

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.