First time update,with the first test's record

This commit is contained in:
Bigsk 2021-07-18 14:39:21 +08:00
commit a4e274316c
14 changed files with 444 additions and 0 deletions

18
code/R.R Normal file
View File

@ -0,0 +1,18 @@
#!/usr/bin/env Rscript
calc <- function(n){
if (n <= 1){
return(n)
}else{
return(calc(n - 1) + calc(n - 2))
}
}
main <- function(){
ts=proc.time()
for ( i in seq(from = 0, to = 29, by = 1)){
calc(i)
#print(calc(i))
}
tf=proc.time()
print(paste0("[",(tf-ts)[3][[1]],"]"))
}
main()

22
code/c.c Normal file
View File

@ -0,0 +1,22 @@
#include<stdio.h>
#include<sys/time.h>
long calc(long n){
if (n <= 1){
return n;
}else{
return (calc(n - 1) + calc(n - 2));
}
}
int main(){
struct timeval tv1, tv2;
gettimeofday(&tv1, NULL);
for (int i=0;i<30;i++){
calc(i);
//printf("%ld\n",calc(i));
//fflush(stdout);
}
gettimeofday(&tv2, NULL);
printf("[%f]\n",(double)(tv2.tv_sec-tv1.tv_sec)+(double)(tv2.tv_usec-tv1.tv_usec)/1000000);
return 0;
}

23
code/cpp.cpp Normal file
View File

@ -0,0 +1,23 @@
#include<iostream>
#include<sys/time.h>
using namespace std;
long calc(long n){
if (n <= 1){
return n;
}else{
return (calc(n - 1) + calc(n - 2));
}
}
int main(){
struct timeval tv1, tv2;
gettimeofday(&tv1, NULL);
for (int i=0;i<30;i++){
//cout<<calc(i)<<endl;
calc(i);
}
gettimeofday(&tv2, NULL);
cout<<"["<<((double)(tv2.tv_sec-tv1.tv_sec)+(double)(tv2.tv_usec-tv1.tv_usec)/1000000)<<"]"<<endl;
return 0;
}

21
code/go.go Normal file
View File

@ -0,0 +1,21 @@
package main
import (
"fmt"
"time"
)
func calc(n int) int {
if (n <= 1){
return n
}
return (calc(n - 1) + calc(n - 2))
}
func main() {
ts:=time.Now().UnixNano()
for i:=0;i<30;i++{
//fmt.Println(calc(i))
calc(i)
}
fmt.Printf("["+fmt.Sprint(float64((time.Now().UnixNano()-ts))/1000000000.0)+"]")
}

18
code/java.java Normal file
View File

@ -0,0 +1,18 @@
public class MainClass {
public static void main(String[] args) {
long startTime=System.nanoTime();
for (int counter = 0; counter < 30; counter++){
calc(counter);
//System.out.printf("%d\n", calc(counter));
}
long endTime=System.nanoTime();
System.out.printf("[%f]\n",(endTime-startTime)/1000000000.0);
}
public static long calc(long n) {
if (n <= 1)
return n;
else
return (calc(n - 1) + calc(n - 2));
}
}

16
code/javascript.js Normal file
View File

@ -0,0 +1,16 @@
function calc(n){
if (n <= 1){
return n;
}
return (calc(n - 1) + calc(n - 2));
}
function main(){
var ts = new Date();
for (i=0;i<30;i++){
//console.log(calc(i));
calc(i)
}
var tf = new Date();
console.log("["+((tf-ts)/1000)+"]");
}
main()

17
code/lua.lua Normal file
View File

@ -0,0 +1,17 @@
function calc(n)
if n <= 1 then
return n
else
return calc(n - 1) + calc(n - 2)
end
end
function main()
local ts = os.clock()
for i=0,29,1 do
--print(calc(i))
calc(i)
end
local tf = os.clock()
print(string.format("[%f]",tf-ts))
end
main()

18
code/perl.pl Normal file
View File

@ -0,0 +1,18 @@
sub calc{
if ($_[0] <= 1){
return $_[0];
}
return (calc($_[0] - 1) + calc($_[0] - 2));
}
sub main{
use Time::HiRes qw(gettimeofday tv_interval);
my $start_time = [gettimeofday];
for ($i=0;$i<30;$i=$i+1){
#print calc($i);
#print "\n";
calc($i)
}
my $interval=tv_interval($start_time,[gettimeofday]);
print "[$interval]\n";
}
main()

22
code/php.php Normal file
View File

@ -0,0 +1,22 @@
<?php
function calc($n){
if ($n <= 1){
return $n;
}else{
return (calc($n - 1) + calc($n - 2));
}
}
function main(){
$ts=microtime();
for ($i=0;$i<30;$i++){
//echo calc($i);
calc($i);
}
$used=microtime()-$ts;
if ($used<0){
$used=0;
}
echo "[".$used."]\n";
}
@main()
?>

13
code/python.py Normal file
View File

@ -0,0 +1,13 @@
import time
def calc(n):
if n <= 1:
return n
else:
return (calc(n - 1) + calc(n - 2))
def main():
ts=time.time()
for i in range(0,30):
#print(calc(i))
calc(i)
print("[{}]".format(time.time()-ts))
main()

19
code/ruby.rb Normal file
View File

@ -0,0 +1,19 @@
require "time"
def calc(n)
if n <= 1
return n
else
return calc(n - 1) + calc(n - 2)
end
end
def main()
ts=Time.now
for i in 0..29
#puts i
#puts calc(i)
calc(i)
end
tf=Time.now
puts "["+(tf-ts).to_s+"]"
end
main()

Binary file not shown.

Binary file not shown.

237
main.py Normal file
View File

@ -0,0 +1,237 @@
import os,threading,shutil,json,time
source="code"
def testc():
shutil.copy(source+"/c.c","tmp/c.c")
time.sleep(1)
os.popen("gcc tmp/c.c -o tmp/testc")
time.sleep(1)
result=[]
for i in range(0,100):
value=json.loads(os.popen("./tmp/testc").read())[0]
print("C completed once used {}s".format(value))
result.append(value)
print("C completed all tasks used {}s".format(sum(result)))
with open("rec/c.txt","a+") as fb:
fb.write(json.dumps(result))
fb.write("\nTotally used:{} Average:{}".format(sum(result),sum(result)/len(result)))
fb.write("\n----------------\n")
def testcpp():
shutil.copy(source+"/cpp.cpp","tmp/cpp.cpp")
time.sleep(1)
os.popen("g++ tmp/cpp.cpp -o tmp/testcpp")
time.sleep(1)
result=[]
for i in range(0,100):
value=json.loads(os.popen("./tmp/testcpp").read())[0]
print("C++ completed once used {}s".format(value))
result.append(value)
print("C++ completed all tasks used {}s".format(sum(result)))
with open("rec/cpp.txt","a+") as fb:
fb.write(json.dumps(result))
fb.write("\nTotally used:{} Average:{}".format(sum(result),sum(result)/len(result)))
fb.write("\n----------------\n")
def testjava():
shutil.copy(source+"/java.java","tmp/java.java")
time.sleep(1)
result=[]
for i in range(0,100):
value=json.loads(os.popen("java tmp/java.java").read())[0]
print("Java completed once used {}s".format(value))
result.append(value)
print("Java completed all tasks used {}s".format(sum(result)))
with open("rec/java.txt","a+") as fb:
fb.write(json.dumps(result))
fb.write("\nTotally used:{} Average:{}".format(sum(result),sum(result)/len(result)))
fb.write("\n----------------\n")
def testgo():
shutil.copy(source+"/go.go","tmp/go.go")
time.sleep(1)
os.popen("go build -o tmp/go tmp/go.go")
time.sleep(1)
result=[]
for i in range(0,100):
value=json.loads(os.popen("./tmp/go").read())[0]
print("Go completed once used {}s".format(value))
result.append(value)
print("Go completed all tasks used {}s".format(sum(result)))
with open("rec/go.txt","a+") as fb:
fb.write(json.dumps(result))
fb.write("\nTotally used:{} Average:{}".format(sum(result),sum(result)/len(result)))
fb.write("\n----------------\n")
def testjs():
shutil.copy(source+"/javascript.js","tmp/javascript.js")
time.sleep(1)
result=[]
for i in range(0,100):
value=json.loads(os.popen("node tmp/javascript.js").read())[0]
print("Node completed once used {}s".format(value))
result.append(value)
print("Node completed all tasks used {}s".format(sum(result)))
with open("rec/node.txt","a+") as fb:
fb.write(json.dumps(result))
fb.write("\nTotally used:{} Average:{}".format(sum(result),sum(result)/len(result)))
fb.write("\n----------------\n")
def testlua():
shutil.copy(source+"/lua.lua","tmp/lua.lua")
time.sleep(1)
result=[]
for i in range(0,100):
value=json.loads(os.popen("lua tmp/lua.lua").read())[0]
print("Lua completed once used {}s".format(value))
result.append(value)
print("Lua completed all tasks used {}s".format(sum(result)))
with open("rec/lua.txt","a+") as fb:
fb.write(json.dumps(result))
fb.write("\nTotally used:{} Average:{}".format(sum(result),sum(result)/len(result)))
fb.write("\n----------------\n")
def testlua():
shutil.copy(source+"/lua.lua","tmp/lua.lua")
time.sleep(1)
result=[]
for i in range(0,100):
value=json.loads(os.popen("lua tmp/lua.lua").read())[0]
print("Lua completed once used {}s".format(value))
result.append(value)
print("Lua completed all tasks used {}s".format(sum(result)))
with open("rec/lua.txt","a+") as fb:
fb.write(json.dumps(result))
fb.write("\nTotally used:{} Average:{}".format(sum(result),sum(result)/len(result)))
fb.write("\n----------------\n")
def testperl():
shutil.copy(source+"/perl.pl","tmp/perl.pl")
time.sleep(1)
result=[]
for i in range(0,100):
value=json.loads(os.popen("perl tmp/perl.pl").read())[0]
print("Perl completed once used {}s".format(value))
result.append(value)
print("Perl completed all tasks used {}s".format(sum(result)))
with open("rec/perl.txt","a+") as fb:
fb.write(json.dumps(result))
fb.write("\nTotally used:{} Average:{}".format(sum(result),sum(result)/len(result)))
fb.write("\n----------------\n")
def testphp():
shutil.copy(source+"/php.php","tmp/php.php")
time.sleep(1)
result=[]
for i in range(0,100):
value=json.loads(os.popen("php tmp/php.php").read())[0]
print("PHP completed once used {}s".format(value))
result.append(value)
print("PHP completed all tasks used {}s".format(sum(result)))
with open("rec/php.txt","a+") as fb:
fb.write(json.dumps(result))
fb.write("\nTotally used:{} Average:{}".format(sum(result),sum(result)/len(result)))
fb.write("\n----------------\n")
def testR():
shutil.copy(source+"/R.R","tmp/R.R")
time.sleep(1)
result=[]
for i in range(0,100):
value=json.loads(os.popen("./tmp/R.R").read()[5:-2])[0]
print("R completed once used {}s".format(value))
result.append(value)
print("R completed all tasks used {}s".format(sum(result)))
with open("rec/R.txt","a+") as fb:
fb.write(json.dumps(result))
fb.write("\nTotally used:{} Average:{}".format(sum(result),sum(result)/len(result)))
fb.write("\n----------------\n")
def testruby():
shutil.copy(source+"/ruby.rb","tmp/ruby.rb")
time.sleep(1)
result=[]
for i in range(0,100):
value=json.loads(os.popen("ruby tmp/ruby.rb").read())[0]
print("Ruby completed once used {}s".format(value))
result.append(value)
print("Ruby completed all tasks used {}s".format(sum(result)))
with open("rec/ruby.txt","a+") as fb:
fb.write(json.dumps(result))
fb.write("\nTotally used:{} Average:{}".format(sum(result),sum(result)/len(result)))
fb.write("\n----------------\n")
def testpython2():
shutil.copy(source+"/python.py","tmp/python2.py")
time.sleep(1)
result=[]
for i in range(0,100):
value=json.loads(os.popen("python tmp/python2.py").read())[0]
print("Python2 completed once used {}s".format(value))
result.append(value)
print("Python2 completed all tasks used {}s".format(sum(result)))
with open("rec/python2.txt","a+") as fb:
fb.write(json.dumps(result))
fb.write("\nTotally used:{} Average:{}".format(sum(result),sum(result)/len(result)))
fb.write("\n----------------\n")
def testpython3():
shutil.copy(source+"/python.py","tmp/python3.py")
time.sleep(1)
result=[]
for i in range(0,100):
value=json.loads(os.popen("python3 tmp/python3.py").read())[0]
print("Python3 completed once used {}s".format(value))
result.append(value)
print("Python3 completed all tasks used {}s".format(sum(result)))
with open("rec/python3.txt","a+") as fb:
fb.write(json.dumps(result))
fb.write("\nTotally used:{} Average:{}".format(sum(result),sum(result)/len(result)))
fb.write("\n----------------\n")
def testpypy():
shutil.copy(source+"/python.py","tmp/pypy.py")
time.sleep(1)
result=[]
for i in range(0,100):
value=json.loads(os.popen("pypy tmp/pypy.py").read())[0]
print("Pypy completed once used {}s".format(value))
result.append(value)
print("Pypy completed all tasks used {}s".format(sum(result)))
with open("rec/pypy.txt","a+") as fb:
fb.write(json.dumps(result))
fb.write("\nTotally used:{} Average:{}".format(sum(result),sum(result)/len(result)))
fb.write("\n----------------\n")
def testjython():
shutil.copy(source+"/python.py","tmp/jython.py")
time.sleep(1)
result=[]
for i in range(0,100):
value=json.loads(os.popen("jython tmp/jython.py").read())[0]
print("Jython completed once used {}s".format(value))
result.append(value)
print("Jython completed all tasks used {}s".format(sum(result)))
with open("rec/jython.txt","a+") as fb:
fb.write(json.dumps(result))
fb.write("\nTotally used:{} Average:{}".format(sum(result),sum(result)/len(result)))
fb.write("\n----------------\n")
def t1():
testcpp()
testjava()
testgo()
testlua()
testjython()
testperl()
testjs()
def t2():
testphp()
testR()
testruby()
testpython2()
testc()
testpython3()
testpypy()
testperl()
def main():
os.makedirs("rec",exist_ok=True)
try:
shutil.rmtree("tmp")
except:
pass
os.makedirs("tmp")
#threading.Thread(target=t1).start()
#threading.Thread(target=t2).start()
input()
testgo()
testlua()
testjython()
testperl()
testjs()
main()