分享
  1. 首页
  2. 文章

Go语言和ASP.NET的一般处理程序在处理WEB请求时的速度比较

songxingzhu · · 2903 次点击 · · 开始浏览
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

Go语言和ASP.NET的一般处理程序在处理WEB请求时的速度比较

1、首先写一个Go语言的简单WEB程序,就返回一个HelloWord!

package main
import (
 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倍!!!


有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

关注微信
2903 次点击
暂无回复
添加一条新回复 (您需要 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传

用户登录

没有账号?注册
(追記) (追記ここまで)

今日阅读排行

    加载中
(追記) (追記ここまで)

一周阅读排行

    加载中

关注我

  • 扫码关注领全套学习资料 关注微信公众号
  • 加入 QQ 群:
    • 192706294(已满)
    • 731990104(已满)
    • 798786647(已满)
    • 729884609(已满)
    • 977810755(已满)
    • 815126783(已满)
    • 812540095(已满)
    • 1006366459(已满)
    • 692541889

  • 关注微信公众号
  • 加入微信群:liuxiaoyan-s,备注入群
  • 也欢迎加入知识星球 Go粉丝们(免费)

给该专栏投稿 写篇新文章

每篇文章有总共有 5 次投稿机会

收入到我管理的专栏 新建专栏