总结:
- luajit性能略逊于go性能
- c/c++ 性能是go的3~4倍
一、字符串查找性能比较
luajit
begin = os.clock();
str = "Hello! My name is Jack. What is your name?"
i = 0
while i <= 500000000 do
string.find(str,"Jack")
i=i+1;
end
print('The program use ', os.clock()-begin, 's');
结果
root@fanpf-VirtualBox:/usr/local/openresty/luajit/bin# ./luajit pfm.lua
The program use 0.544375 s
耗时:0.544375
C && CPP
#include <string>
#include <chrono>
#include <iostream>
#include <string.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
using namespace std;
void test_c(size_t count)
{
char str1[] = "Hello! My name is Jack. What is your name?";
char str2[] = "Jack";
time_t begin, over;
char *pos;
size_t i = 0;
time(&begin);
while (i <= count) {
pos = strstr(str1,str2);
i++;
}
time(&over);
printf("%s:The program use %lu s \n", __func__, over - begin);
}
void test_cpp_strfind(size_t count)
{
string str1 = "Hello! My name is Jack. What is your name?";
string str2 = "Jack";
time_t end, start = chrono::system_clock::to_time_t(chrono::system_clock::now());
size_t i = 0;
while (i <= count) {
str1.find(str2);
i++;
}
end = chrono::system_clock::to_time_t(chrono::system_clock::now());
printf("%s: The program use %d s \n", __func__, static_cast<int>(end - start));
}
void test_cpp_strview_find(size_t count)
{
string_view str1 = "Hello! My name is Jack. What is your name?";
string_view str2 = "Jack";
time_t end, start = chrono::system_clock::to_time_t(chrono::system_clock::now());
size_t i = 0;
while (i <= count) {
str1.find(str2);
i++;
}
end = chrono::system_clock::to_time_t(chrono::system_clock::now());
printf("%s: The program use %d s \n", __func__, static_cast<int>(end - start));
}
void test_cpp_strstr(size_t count)
{
string str1 = "Hello! My name is Jack. What is your name?";
string str2 = "Jack";
time_t start = chrono::system_clock::to_time_t(chrono::system_clock::now());
size_t i = 0;
while (i <= count) {
strstr(str1.c_str(),str2.c_str());
i++;
}
time_t end = chrono::system_clock::to_time_t(chrono::system_clock::now());
printf("%s The program use %d s \n", __func__, static_cast<int>(end - start));
}
int main()
{
size_t count = 500000000;
test_c(count);
test_cpp_strstr(count);
test_cpp_strfind(count);
test_cpp_strview_find(count);
return 0;
}
int i = 0;
time(&begin);
while (i <= count) {
strstr(str1,str2);
i++;
}
time(&over);
printf("%s:The program use %d s \n", __func__, (int)(over - begin));
}
void test_cpp_strfind(int count)
{
string str1 = "Hello! My name is Jack. What is your name?";
string str2 = "Jack";
time_t end, start = chrono::system_clock::to_time_t(chrono::system_clock::now());
int i = 0;
while (i <= count) {
str1.find(str2);
i++;
}
end = chrono::system_clock::to_time_t(chrono::system_clock::now());
printf("%s: The program use %d s \n", __func__, static_cast<int>(end - start));
}
void test_cpp_strstr(int count)
{
string str1 = "Hello! My name is Jack. What is your name?";
string str2 = "Jack";
time_t start = chrono::system_clock::to_time_t(chrono::system_clock::now());
int i = 0;
while (i <= count) {
strstr(str1.c_str(),str2.c_str());
i++;
}
time_t end = chrono::system_clock::to_time_t(chrono::system_clock::now());
printf("%s The program use %d s \n", __func__, static_cast<int>(end - start));
}
int main()
{
int count = 500000000;
test_c(count);
test_cpp_strstr(count);
test_cpp_strfind(count);
/*
test_c:The program use 1 s
test_cpp_strstr The program use 8 s
test_cpp_strfind: The program use 10 s
*/
return 0;
}
不使用-O2 结果
使用-O2后的执行结果:
test_c:The program use 0 s
test_cpp_strstr The program use 0 s
test_cpp_strfind: The program use 9 s
test_cpp_strview_find: The program use 0 s
stringview效率比string高很多,-O2选项提高运行效率
golang
package main
import (
"fmt"
"strings"
"time"
)
func main() {
count := 500000000
str1 := "Hello! My name is Jack. What is your name?"
str2 := "Jack"
start := time.Now().UnixNano()
for i := 0; i <= count; i++ {
strings.Contains(str1, str2)
}
end := time.Now().UnixNano()
fmt.Println("use:\n", (end-start)/1000)
}
结果: 用时4s
字符串测试总结:
- c耗时:0秒
- cpp + strstr耗时: 0秒
- cpp + stringview: 0秒
- luajit耗时:0.544375秒
- go耗时:4秒
- cpp string耗时:9秒
Fibonacci测试
lua
--递归
local function Fibonacci_1(n)
if n == 1 or n == 2 then
return 1
else
return Fibonacci_1(n - 1) + Fibonacci_1(n - 2)
end
end
Fibonacci_1(45)
root@fanpf-VirtualBox:/usr/local/openresty/luajit/bin/fib# time ../luajit fib.lua
real 0m7,219s
user 0m7,231s
sys 0m0,000s
c
#include <stdio.h>
int fibonacci(int i){
if(i<2) return i;
return fibonacci(i-2) + fibonacci(i-1);
}
int main(){
printf("%d",fibonacci(45));
}
real 0m1,823s
cpp
#include <iostream>
constexpr int fibonacci(const int i){
if(i<2) return i;
return fibonacci(i-2) + fibonacci(i-1);
}
int main() {
fibonacci(45);
return 0;
}
real 0m1,536s
go
package main
import "fmt"
func main(){
fmt.Println(fibonacci(45))
}
func fibonacci(i int) int{
if(i<2){
return i;
}
return fibonacci(i-2) + fibonacci(i-1);
}
real 0m6,517s
fib计算总结:cpp最快, lua最慢
- cpp:0m1,536s
- C: 0m1,823s
- go:0m6,517s
- luajit:0m7,219s