package main;
import "fmt"
import "time"
type Data struct
x float64
y float64
a int
func MakeData(num int) []Data
var vec = make([]Data, 0, num)
for i:=0; i
var data Data
data.x = float64(i) + 0.5;
data.y = float64(i) + 1.5;
data.a = i;
vec = append(vec, data)
return vec
func Cal(data []Data, idx int, num int) float64
var sum1 float64 = 0.0;
var sum2 float64 = 0.0;
for i:= idx-num+1; i <= idx; i++
if i <0
continue;
var elem = data[i];
sum1 += elem.x;
sum2 += elem.y;
var avg1 = sum1/float64(num);
var avg2 = sum2/float64(num);
return (avg1 + avg2)/2;
func Make(data []Data)
var target = make([]float64, 0, len(data));
for i := 0; i
var v = Cal(data, i, 1000);
if v > 1000
target = append(target, v)
fmt.Println("target size:" , len(target))
func main()
var t1 = time.Now().UnixNano()
var data = MakeData(300*365*5);
Make(data);
var t2 = time.Now().UnixNano()
fmt.Println("use time:", float64(t2-t1)/1000000000)
#include
#include
#include
#include
#include <string>
#include
#include
struct Data
double x;
double y;
int a;
;
std::vector MakeData(int num)
std::vector vec;
vec.reserve(num);
for (int i=0; i)
Data data;
data.x = static_cast<double>(i) + 0.5;
data.y = static_cast<double>(i) + 1.5;
data.a = i;
vec.push_back(std::move(data));
return std::move(vec);
double Cal(std::vector & data, int idx, int num)
double sum1 = 0.0;
double sum2 = 0.0;
for (int i = idx-num+1; i <= idx; i++)
if (i <0)
continue;
auto & elem = data[i];
sum1 += elem.x;
sum2 += elem.y;
auto avg1 =sum1/num;
auto avg2 =sum2/num;
return (avg1 + avg2)/2;
void Make(std::vector & data)
std::vector<double> target;
target.reserve(data.size());
for (int i = 0; i )
auto v = Cal(data, i, 1000);
if (v > 1000)
target.push_back(v);
std::cout <<"target size:" < std::endl;
int main(int argc,char** argv)
struct timeval t1;
struct timeval t2;
gettimeofday(&t1, NULL);
auto data = MakeData(300*365*5);
Make(data);
gettimeofday(&t2, NULL);
auto usetime = double((t2.tv_sec*1000000 + t2.tv_usec) - (t1.tv_sec*1000000 + t1.tv_usec))/1000000;
std::cout <<"use time: " < std::endl;
class Data
constructor()
this.x = 0.0;
this.y = 0.0;
this.a = 0;
;
function MakeData(num)
let vec = [];
for (let i=0; i)
let data = new Data();
data.x = i + 0.5;
data.y = i + 1.5;
data.a = i;
vec.push(data);
return vec;
function Cal(data, idx, num)
let sum1 = 0.0;
let sum2 = 0.0;
for (let i = idx-num+1; i <= idx; i++)
if (i <0)
continue;
let elem = data[i];
sum1 += elem.x;
sum2 += elem.y;
let avg1 =sum1/num;
let avg2 =sum2/num;
return (avg1 + avg2)/2;
function Make(data)
let target = [];
for (let i = 0; i )
let v = Cal(data, i, 1000);
if (v > 1000)
target.push(v);
console.log("target size:", target.length);
t1 = new Date().getTime();
let data = MakeData(300*365*5);
Make(data);
t2= new Date().getTime();
console.log("use time:", (t2-t1)/1000)
import time
class Data:
def __init__(self):
self.x = 0.0
self.y = 0.0
self.a = 0
def MakeData(num):
vec = []
for i in range(0, num):
data = Data()
data.x = i + 0.5
data.y = i + 1.5
data.a = i
vec.append(data)
return vec
def Cal(data, idx, num):
sum1 = 0.0
sum2 = 0.0
i = idx-num+1
while i<=idx:
if i <0:
i+=1
continue
elem = data[i]
sum1 += elem.x
sum2 += elem.y
i+=1
avg1 =sum1/num
avg2 =sum2/num
return (avg1 + avg2)/2
def Make(data):
target = []
data_len = len(data)
for i in range(0, data_len):
v = Cal(data, i, 1000)
if v > 1000:
target.append(v)
print("target size:" , len(target))
t1=time.time()
data = MakeData(300*365*5)
Make(data)
print("use time:", time.time() - t1)