c# go for循环性能对比

9 篇文章 0 订阅

C#

{
        static void Main(string[] args)
        {

            TestPerformance();
            Console.ReadKey();
        }

        static void TestPerformance()
        {
            long num = 500000000;
            long count = 0;
            DateTime _startTime = DateTime.UtcNow;
            Console.WriteLine(_startTime.ToString());
            for(int i=0;i<num;i++)
            {
                if(i%2==0)
                {
                    count++;
                }
            }
            DateTime _endTime = DateTime.UtcNow;
            Console.WriteLine(_endTime.ToString());
            Console.WriteLine("Process Loop:"+count+",Spend Mill Seconds: "+(_endTime-_startTime).TotalMilliseconds);
        }
}
# 运行于netcore,1154毫秒 DEBUG
#2019/4/30 8:38:47
#2019/4/30 8:38:48
#Process Loop:250000000,Spend Mill Seconds: 1154.9266
-----------------------
#参考回复,Release下,不使用stopwatch,时间大概500ms,使用stopwatch调整如下,相比go还是有一点点差距(i5 8600k)
#Process Loop:250000000,Spend Mill Seconds: 364

Go

package main
import "time"
import "fmt"
func main(){
	TestPerformance()
}
func TestPerformance(){
	var num,count,i int64
	num=500000000
	//fmt.Println(time.Now())
	st:=time.Now().UnixNano()/1e6
	fmt.Println(st)
	for i=0;i<num;i++{
		if 1%2==0{
			count++;
		}
	}
	//fmt.Println(time.Now())
	et:=time.Now().UnixNano()/1e6
	fmt.Println(et)
	fmt.Println((et-st))
}

#go 结果
#1556613847488
#1556613847612
#124

go相较C#快十倍

 

C# DateTime与时间戳转换https://www.cnblogs.com/polk6/p/6024892.html

JavaScript时间戳:是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总毫秒数。

Unix时间戳:是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。

Golang的time包:秒、毫秒、纳秒时间戳输出https://blog.csdn.net/mirage003/article/details/80822608
10位数的时间戳是以 秒 为单位;13位数的时间戳是以 毫秒 为单位;19位数的时间戳是以 纳秒 为单位;

golang中可以这样写:

package main

import (
    "time"
    "fmt"
)

func main() {
    fmt.Printf("时间戳(秒):%v;\n", time.Now().Unix())
    fmt.Printf("时间戳(纳秒):%v;\n",time.Now().UnixNano())
    fmt.Printf("时间戳(毫秒):%v;\n",time.Now().UnixNano() / 1e6)
    fmt.Printf("时间戳(纳秒转换为秒):%v;\n",time.Now().UnixNano() / 1e9)
}

#输出结果为:
#时间戳(秒):1530027865;
#时间戳(纳秒):1530027865231834600;
#时间戳(毫秒):1530027865231;
#时间戳(纳秒转换为秒):1530027865;

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值