Now find c First of all ,python second ,go Third
Testing capabilities : Seek before 100000 What are the middle primes , How many
Here's how long the test took ,go The slowest
c :1.25s
python: 1.36s
go: 3.6s
go How could it be the third ? Is there anyone who can help me optimize ...
python Of course, there are other optimization schemes , Used here numba, Compatibility is poor , There are many mistakes when using it . It can be used pypy, Of course, it can also be used nuitka Compile to binary , however bumba It's the fastest .
use cpython, want 30 More than a second ,python After optimization :
use nuitka It seems that it only takes a few seconds 8、9 second .
use pypy Probably 4.5 About seconds .
use numba only 1.36 second .
numba So fast , If the compatibility is higher, it will be invincible .
python It's better than that go Come on so much ?
original 2021-07-29 11:55· A procedural ape mistaken for eating goods
I tested c、python、go Performance of
Now find c First of all ,python second ,go Third
Testing capabilities : Seek before 100000 What are the middle primes , How many
Here's how long the test took ,go The slowest
c :1.25s
python: 1.36s
go: 3.6s
go How could it be the third ? Is there anyone who can help me optimize ...
python Of course, there are other optimization schemes , Used here numba, Compatibility is poor , There are many mistakes when using it . It can be used pypy, Of course, it can also be used nuitka Compile to binary , however bumba It's the fastest .
use cpython, want 30 More than a second ,python After optimization :
use nuitka It seems that it only takes a few seconds 8、9 second .
use pypy Probably 4.5 About seconds .
use numba only 1.36 second .
numba So fast , If the compatibility is higher, it will be invincible .
The surprise is :main Which code in the function , It can be used github copilot complete , Just press tab, enter , You can do it ..
It's terrible to think about it
c The code is as follows :
#include <stdio.h>
#include <stdint.h>
#include <sys/time.h>
int is_prime(long n) {
long i = 2L;
while (i < n) {
if (n % i == 0) {
return 0;
}
i += 1;
}
return 1;
}
uint64_t elapsed(struct timeval start, struct timeval end) {
return (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec);
}
int main() {
int prime_num = 0;
struct timeval start, end;
gettimeofday(&start, 0);
for (long i = 0; i < 100000; i ++) {
if (is_prime(i)) {
prime_num += 1;
}
}
gettimeofday(&end, 0);
uint64_t elapsed_time = elapsed(start, end);
printf("time cost:%fs, prime_num:%ld\n", elapsed_time/1000000.0, prime_num);
return 0;
}
python The code is as follows :
#-*-coding:utf-8-*-
import time
import numba
@numba.jit
def is_prime(n):
i = 2
while i < n:
if n % i == 0:
return False
i += 1
return True
def test():
prime_num = 0
t1 = time.clock()
for i in range(2, 100000):
if is_prime(i):
prime_num += 1
t2 = time.clock()
print ("time cost:{}s, prime_num:{}".format(t2-t1, prime_num))
if __name__ == '__main__':
test()
go The code is as follows :
package main
import (
"fmt"
"time"
)
func is_prime(n int) bool {
var i = 2
for i < n {
if n%i == 0 {
return false
}
i++
}
return true
}
func main() {
t1 := time.Now()
prime_num := 0
count := 100000
fori := 0; i < count; i++ {
if is_prime(i) {
prime_num++
}
}
t2 := time.Since(t1)
fmt.Println("time cost:", t2, "prime_num:", prime_num)
}
Learn from good examples Python Whether it's employment or sideline, it's good to make money , But learn to Python Still have a learning plan . Finally, let's share a complete set of Python Learning materials , For those who want to learn Python Let's have a little help !
Python The technical points in all directions are sorted out , Form a summary of knowledge points in various fields , The use of it is , You can find the corresponding learning resources according to the above knowledge points , Make sure you learn more comprehensively .( At the end of the article !)
reminder : Limited space , Packaged folder , The way to get it is “ At the end of the article ”!!!
Watch the zero basics learning video , Watching video learning is the quickest and most effective way , Follow the teacher's ideas in the video , From foundation to depth , It's still easy to get started .
Optical theory is useless , Learn to knock together , Do it , Can you apply what you have learned to practice , At this time, we can make some practical cases to learn .
Check the learning results .
We learn Python Must be to find a well paid job , The following interview questions are from Ali 、 tencent 、 The latest interview materials of big Internet companies such as byte , And the leader Ali gave an authoritative answer , After brushing this set of interview materials, I believe everyone can find a satisfactory job .
Guarantee 100% free
】I am 18 Self study in Python,
use python3.9 Written Linux Sc