The computation speed of Python, C #, Golang, and Pypy is irresponsible,

Source: Internet
Author: User

The computation speed of Python, C #, Golang, and Pypy is irresponsible,

Recently I have been studying C # And Golang and want to compare their performance.

The code is poorly written for reference only.

Modify the code from http://www.linuxidc.com/linux/2014-08/105982.htm.

 

First go to C #

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 7 namespace Test 8 {9 class Program10 {11 static void Main (string [] args) 12 {13 System. diagnostics. stopwatch stopwatch = new System. diagnostics. stopwatch (); 14 stopwatch. start (); // Start monitoring code 15 int range = (int) Math. pow (10, 7); 16 Console. writeLine ("{0} {1}", range, range); 17 IList <int> listA = new List <int> (); 18 for (int I = 0; I <range; I ++) 19 {20 listA. add (I + 1); 21} 22 foreach (int item in listA) 23 {24 if (Check (item) 25 {26 double sqitem = Math. pow (item, 2); 27 if (Check (sqitem) {Console. writeLine ("{0} {1}", item, sqitem) ;}28} 29} 30 stopwatch. stop (); // Stop monitoring 31 TimeSpan timeSpan = stopwatch. elapsed; // get the total time 32 int hours = Convert. toInt32 (timeSpan. totalHours); // 33 int minutes = Convert. toInt32 (timeSpan. totalMinutes); // minute 34 int seconds = Convert. toInt32 (timeSpan. totalSeconds); // number of seconds 35 double milliseconds = timeSpan. totalMilliseconds; // 36 consoles in milliseconds. writeLine ("Total time {0}, hour {1}, minute {2}, number of seconds {3}, millisecond count {4}", timeSpan, hours, minutes, seconds, milliseconds); 37 Console. readLine (); 38} 39 40 static bool Check (int num) {41 int a = num; 42 char [] B = Convert. toString (). toCharArray (); 43 Array. reverse (B); 44 string c = Convert. toString (a); 45 string d = new string (B); 46 return c = d; 47} 48 static bool Check (double num) {49 double a = num; 50 char [] B = Convert. toString (). toCharArray (); 51 Array. reverse (B); 52 string c = Convert. toString (a); 53 string d = new string (B); 54 return c = d; 55} 56} 57}
View Code

Go to Golang again

 1 package main 2 import ( 3     "fmt" 4     "time" 5     "math" 6     "strconv" 7 ) 8 func main() { 9     defer timeCost(time.Now())10     var max = math.Pow(10, 7)11     var all = makeRange(1, int(max))12     for i := range all {13         if check(i) {14             j := math.Pow(float64(i), 2)15             if check(int(j)) {16                 fmt.Println(i, j)17             }18         }19     }20 }21 22 func check(num int) bool {23     a := strconv.Itoa(num)24     b := Reverse(a)25     return a == b26 }27 28 func Reverse(s string) string {29     runes := []rune(s)30     for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {31         runes[i], runes[j] = runes[j], runes[i]32     }33     return string(runes)34 }35 36 func timeCost(start time.Time){37     terminal:=time.Since(start)38     fmt.Println(terminal)39     var str string40     fmt.Scanf("%s", str)41 }42 43 func makeRange(min, max int) []int {44     a := make([]int, max-min+1)45     for i := range a {46         a[i] = min + i47     }48     return a49 }
View Code

Python and Pypy

 1 import time 2  3 def check(num): 4     a = list(str(num)) 5     b = a[::-1] 6     return a == b 7  8 def main(): 9     all = xrange(1, 10**7)10     for i in all:11         if check(i):12             if check(i**2):13                 print i, i**214     print time.time() - t15 16 if __name__ == '__main__':17     t = time.time()18     main()
View Code

 

Test results:

  ThinkPad T450 Macbook 12"
C # 5S 9 s (mono)
Go 1.7.3 3 s 3 s
Python 1, 2.7 13 s 17 s
Pypy 5.7.1 Not tested 2 s

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.