The V Programming Language

Simple, fast, safe language created for developing Volt, soon available for everyone.

Open source release in June 2019. Early access since April 15.
Install V from source in 0.5 seconds
wget vlang.io/v.c && gcc -o v v.c
1.9k

Performance compare on loop...

Alex
Apr 15 · 3 min read
templarzq Jun 25 09:28
cpp code:

#include <iostream>
#include <time.h>

using namespace std;

const int MATRIX_SIZE = 32;

int Matrix[MATRIX_SIZE][MATRIX_SIZE];

void MakeMatrix(int rows, int cols, int mx[MATRIX_SIZE][MATRIX_SIZE])
{
rows--;
cols--;
int count = 1;
for (int r = 0; r <= rows; r++)
{
for (int c = 0; c <= cols; c++)
{
mx[r][c] = count;
count++;
}
}
}

void MatrixMult(int rows, int cols, const int m1[MATRIX_SIZE][MATRIX_SIZE], const int m2[MATRIX_SIZE][MATRIX_SIZE], int mx[MATRIX_SIZE][MATRIX_SIZE])
{
rows--;
cols--;

int val;
for (int i = 0; i <= rows; i++)
{
for (int j = 0; j <= cols; j++)
{
val = 0;
for (int k = 0; k <= cols; k++)
{
val += m1[i][k] * m2[k][j];
mx[i][j] = val;
}
}
}
}

int main(int argc, char* argv[])
{
int num = 100000;

int m1[MATRIX_SIZE][MATRIX_SIZE], m2[MATRIX_SIZE][MATRIX_SIZE], mx[MATRIX_SIZE][MATRIX_SIZE];
MakeMatrix(MATRIX_SIZE, MATRIX_SIZE, m1);
MakeMatrix(MATRIX_SIZE, MATRIX_SIZE, m2);

time_t tick = time(NULL);
for (int i = 0; i <= num; i++)
{
MatrixMult(MATRIX_SIZE, MATRIX_SIZE, m1, m2, mx);
}
tick = time(NULL) - tick;
cout << mx[0][0] << " " << mx[2][3] << " " << mx[3][2] << " " << mx[4][4] << " " << mx[29][29] << endl;
cout << tick << " s" << endl;

return 0;
}
templarzq Jun 25 09:29
vlang code:

module main

import time


const (
cSize = 32
)


fn mkmatrix(rows int, cols int, mx []int) {
rows--
cols--
mut count := 1
mut c:=0
for r := 0; r <= rows; r++ {
for c = 0; c <= cols; c++ {
mx[r*cSize+c] = count
count++
}
}
}

fn multmatrix(rows, cols int, m1, m2 []int, mm []int) {
rows--
cols--
mut val :=0
mut j :=0
mut k :=0
for i := 0; i <= rows; i++ {
for j = 0; j <= cols; j++ {
val = 0
for k = 0; k <= cols; k++ {
val += m1[i*cSize+k] * m2[k*cSize+j]
mm[i*cSize+j] = val
}
}
}
}


fn main() {
mut m1 := [0;cSize*cSize]
mut m2 := [0;cSize*cSize]
mut mm := [0;cSize*cSize]


mkmatrix(cSize, cSize, m1)
mkmatrix(cSize, cSize, m2)
//t0 := time.now()
for i := 0; i <= 10000; i++ {
multmatrix(cSize, cSize, &m1, &m2, &mm)
}
//t := time.now()
a1:=mm[0]
a2:=mm[2*32+3]
a3:=mm[3*32+2]
a4:=mm[4*32+4]
a5:=mm[29*32+29]

//usedTime:=(t-t0).format()

println('$a1, $a2, $a3, $a4, $a5')
//println('tick = $usedTime')
}

templarzq Jun 25 09:29
vlang no optimzation for loop at all...
templarzq Jun 25 09:31
compile cmd:
gcc -o cmain -O3 main.cpp //100000 loop for 1s

v -o vmain main.v //10000 loop for 4s

time ./cmain
time ./vmain
templarzq Jun 25 09:46
and i tried this:
v -O3 -o matrixv main.v

no effection LOL
medvednikov Jun 25 11:21
you have to use v -prod
templarzq Jun 26 01:53
good job~ hoping for more improvement
Log in via GitHub to comment



Powered by vtalk, open-source blogging/forum software written in V