A Benchmark of matrix multiplication between C and Python
#Motivation
After a Python convention in my city (Python Brasil) me, a unqualified newbie and a friend of mine from the comp. sci. academia discussed with a few colleagues about the potential advantages of python, including its application in the scientific field for numerical applications.
One of their arguments was that runtime optimization provided by pypy offered a significant advantage over C.
Well without further ado, here are the source codes for each language.
#Source Codes
**python w/ numpy**
#! /usr/bin/python
import sys
import numpy as np
import time
n = int(sys.argv[1])
m1_file = sys.argv[2]
m2_file = sys.argv[3]
m1 = np.loadtxt(m1_file)
m2 = np.loadtxt(m2_file)
m3 = np.zeros(m1.shape)
start = time.time()
np.matmul(m1, m2, m3)
end = time.time()
print m3
print 'Time:', (end - start) * 1000.0
**python**
#! /usr/bin/python2
import sys
import time
n = int(sys.argv[1])
m1_file = sys.argv[2]
m2_file = sys.argv[3]
def readm(filename):
f = open(filename, 'r')
d = f.read()
mat = [[float(i) for i in row] for row in [s.split(' ')[0:n] for s in d.split('\n')[0:n]]]
return mat
m1 = readm(m1_file)
m2 = readm(m2_file)
m3 = [[0 for i in range(n)] for j in range(n)]
start = time.time();
for i in range(n):
for j in range(n):
for k in range(n):
m3[i][j] += (m1[i][k] * m2[k][j])
end = time.time();
for i in range(n):
for j in range(n):
print m3[i][j],
print ''
print 'Time:', (end - start) * 1000.0
**C**
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#ifdef ARRAY
void readm(FILE* f, int n, double* m) {
#endif
#ifdef MATRIX
void readm(FILE* f, int n, double** m) {
#endif
int i, j;
#ifdef ARRAY
for (i = 0; i < n * n; ++i)
fscanf(f, "%lf", &m[i]);
#endif
#ifdef MATRIX
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j)
fscanf(f, "%lf", &m[i][j]);
#endif
}
int main(int argc, char** argv) {
int i, j, k;
double start, end;
struct timeval tv_start, tv_end;
int n = atoi(argv[1]);
FILE* f1 = fopen(argv[2], "r");
FILE* f2 = fopen(argv[3], "r");
#ifdef ARRAY
double* m1 = (double*) malloc(sizeof(double) * n * n);
double* m2 = (double*) malloc(sizeof(double) * n * n);
double* m3 = (double*) malloc(sizeof(double) * n * n);
for (i = 0; i < n * n; ++i) m3[i] = 0;
readm(f1, n, m1);
readm(f2, n, m2);
gettimeofday(&tv_start, NULL);
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j)
for (k = 0; k < n; ++k)
m3[(i * n) + j] += (m1[(i * n) + k] * m2[(k * n) + j]);
gettimeofday(&tv_end, NULL);
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j)
fprintf(stderr, "%lf ", m3[(i * n) + j]);
fprintf(stderr, "\n");
}
#endif
#ifdef MATRIX
double** m1 = (double**) malloc(sizeof(double*) * n);
double** m2 = (double**) malloc(sizeof(double*) * n);
double** m3 = (double**) malloc(sizeof(double*) * n);
for (i = 0; i < n; ++i) {
m1[i] = (double*) malloc(sizeof(double) * n);
m2[i] = (double*) malloc(sizeof(double) * n);
m3[i] = (double*) malloc(sizeof(double) * n);
}
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j)
m3[i][j] = 0;
readm(f1, n, m1);
readm(f2, n, m2);
gettimeofday(&tv_start, NULL);
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j)
for (k = 0; k < n; ++k)
m3[i][j] += (m1[i][k] * m2[k][j]);
gettimeofday(&tv_end, NUL
#Motivation
After a Python convention in my city (Python Brasil) me, a unqualified newbie and a friend of mine from the comp. sci. academia discussed with a few colleagues about the potential advantages of python, including its application in the scientific field for numerical applications.
One of their arguments was that runtime optimization provided by pypy offered a significant advantage over C.
Well without further ado, here are the source codes for each language.
#Source Codes
**python w/ numpy**
#! /usr/bin/python
import sys
import numpy as np
import time
n = int(sys.argv[1])
m1_file = sys.argv[2]
m2_file = sys.argv[3]
m1 = np.loadtxt(m1_file)
m2 = np.loadtxt(m2_file)
m3 = np.zeros(m1.shape)
start = time.time()
np.matmul(m1, m2, m3)
end = time.time()
print m3
print 'Time:', (end - start) * 1000.0
**python**
#! /usr/bin/python2
import sys
import time
n = int(sys.argv[1])
m1_file = sys.argv[2]
m2_file = sys.argv[3]
def readm(filename):
f = open(filename, 'r')
d = f.read()
mat = [[float(i) for i in row] for row in [s.split(' ')[0:n] for s in d.split('\n')[0:n]]]
return mat
m1 = readm(m1_file)
m2 = readm(m2_file)
m3 = [[0 for i in range(n)] for j in range(n)]
start = time.time();
for i in range(n):
for j in range(n):
for k in range(n):
m3[i][j] += (m1[i][k] * m2[k][j])
end = time.time();
for i in range(n):
for j in range(n):
print m3[i][j],
print ''
print 'Time:', (end - start) * 1000.0
**C**
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#ifdef ARRAY
void readm(FILE* f, int n, double* m) {
#endif
#ifdef MATRIX
void readm(FILE* f, int n, double** m) {
#endif
int i, j;
#ifdef ARRAY
for (i = 0; i < n * n; ++i)
fscanf(f, "%lf", &m[i]);
#endif
#ifdef MATRIX
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j)
fscanf(f, "%lf", &m[i][j]);
#endif
}
int main(int argc, char** argv) {
int i, j, k;
double start, end;
struct timeval tv_start, tv_end;
int n = atoi(argv[1]);
FILE* f1 = fopen(argv[2], "r");
FILE* f2 = fopen(argv[3], "r");
#ifdef ARRAY
double* m1 = (double*) malloc(sizeof(double) * n * n);
double* m2 = (double*) malloc(sizeof(double) * n * n);
double* m3 = (double*) malloc(sizeof(double) * n * n);
for (i = 0; i < n * n; ++i) m3[i] = 0;
readm(f1, n, m1);
readm(f2, n, m2);
gettimeofday(&tv_start, NULL);
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j)
for (k = 0; k < n; ++k)
m3[(i * n) + j] += (m1[(i * n) + k] * m2[(k * n) + j]);
gettimeofday(&tv_end, NULL);
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j)
fprintf(stderr, "%lf ", m3[(i * n) + j]);
fprintf(stderr, "\n");
}
#endif
#ifdef MATRIX
double** m1 = (double**) malloc(sizeof(double*) * n);
double** m2 = (double**) malloc(sizeof(double*) * n);
double** m3 = (double**) malloc(sizeof(double*) * n);
for (i = 0; i < n; ++i) {
m1[i] = (double*) malloc(sizeof(double) * n);
m2[i] = (double*) malloc(sizeof(double) * n);
m3[i] = (double*) malloc(sizeof(double) * n);
}
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j)
m3[i][j] = 0;
readm(f1, n, m1);
readm(f2, n, m2);
gettimeofday(&tv_start, NULL);
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j)
for (k = 0; k < n; ++k)
m3[i][j] += (m1[i][k] * m2[k][j]);
gettimeofday(&tv_end, NUL
Flask_wtf custom validator RequiredIf not working with fieldlist / formfields
I'm trying to write a custom validator for flask\_wtf that makes a field required if another field is set to a certain value.
*Example:*
If field A == 'yes' then field B should be required. For all other values of field A no validation should run on field B.
So far i've been able to come up with the following code based on several example's i've found online:
~~~
class RequiredIf(object):
#validator which makes a field required depending on another field
#source: [https://stackoverflow.com/questions/8463209/how-to-make-a-field-conditionally-optional-in-wtforms](https://stackoverflow.com/questions/8463209/how-to-make-a-field-conditionally-optional-in-wtforms)
def __init__(self, other_field_name, trigger, message=None):
self.other_field_name = other_field_name
self.trigger = trigger
if not message:
message = 'Based on your previous input, this field is required.'
self.message = message
def __call__(self, form, field):
other_field = form._fields.get(self.other_field_name)
trigger = self.trigger
if other_field is None:
/r/flask
https://redd.it/nc4ow3
I'm trying to write a custom validator for flask\_wtf that makes a field required if another field is set to a certain value.
*Example:*
If field A == 'yes' then field B should be required. For all other values of field A no validation should run on field B.
So far i've been able to come up with the following code based on several example's i've found online:
~~~
class RequiredIf(object):
#validator which makes a field required depending on another field
#source: [https://stackoverflow.com/questions/8463209/how-to-make-a-field-conditionally-optional-in-wtforms](https://stackoverflow.com/questions/8463209/how-to-make-a-field-conditionally-optional-in-wtforms)
def __init__(self, other_field_name, trigger, message=None):
self.other_field_name = other_field_name
self.trigger = trigger
if not message:
message = 'Based on your previous input, this field is required.'
self.message = message
def __call__(self, form, field):
other_field = form._fields.get(self.other_field_name)
trigger = self.trigger
if other_field is None:
/r/flask
https://redd.it/nc4ow3
Stack Overflow
How to make a field conditionally optional in WTForms?
My form validation is working nearly complete, I just have 2 cases I don't know exactly how to solve: 1) The password field should be required of course but I also provide the possibility to log in...