Commit 7dea6a0e by Patryk Czarnik

Przykłady dot. porównania szybkości

parent 55bfa064
import sys
# najbardziej oczywisty zapis w Pythonie - wersja z tworzeniem tablic int
def mnoz_zakres(n):
t1 = list(range(n))
t2 = list(range(n))
suma = 0
for e1 in t1:
for e2 in t2:
suma += e1 * e2
return suma
if __name__ == '__main__':
n = int(sys.argv[1])
wynik = mnoz_zakres(n)
print('Wynik:', wynik)
import sys
# wersja bez tworzenia tablic
def mnoz_zakres(n):
return sum(e1 * e2 for e1 in range(n) for e2 in range(n))
if __name__ == '__main__':
n = int(sys.argv[1])
wynik = mnoz_zakres(n)
print('Wynik:', wynik)
import sys
import numpy
# wersja z użyciem biblioteki numpy
# Gdy użyjemy tej biblioteki, będą używane "natywne" liczby, podobnie jak w C i Javie
def mnoz_zakres(n):
t1 = numpy.arange(n, dtype=numpy.int64).reshape(1, n)
t2 = numpy.arange(n, dtype=numpy.int64).reshape(n, 1)
return (t2 @ t1).sum()
if __name__ == '__main__':
n = int(sys.argv[1])
#n = 10000
wynik = mnoz_zakres(n)
print('Wynik:', wynik)
#include <stdio.h>
#include <stdlib.h>
long mnoz_zakres(int n) {
int *t1 = (int*) malloc (n * sizeof(int));
int *t2 = (int*) malloc (n * sizeof(int));
for(int i=0; i < n; i++) {
t1[i] = i;
t2[i] = i;
}
long suma = 0;
for(int i=0; i < n; i++) {
for(int j=0; j < n; j++) {
suma += t1[i] * t2[j];
}
}
free(t1);
free(t2);
return suma;
}
int main(int argc, char **argv) {
int n = atoi(argv[1]);
long wynik = mnoz_zakres(n);
printf("Wynik: %ld\n", wynik);
return 0;
}
// Najbardziej oczywista implementacja w Javie
// Wersja z tworzeniem tablic.
public class mnozenie4 {
private static long mnoz_zakres(int n) {
int[] t1 = new int[n];
int[] t2 = new int[n];
for(int i=0; i < n; i++) {
t1[i] = i;
t2[i] = i;
}
long suma = 0;
for(int x : t1) {
for(int y : t2) {
suma += x * y;
}
}
return suma;
}
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
long wynik = mnoz_zakres(n);
System.out.printf("Wynik: %d\n", wynik);
}
}
// Najbardziej oczywista implementacja w Javie
// Wersja bez tworzenia tablic.
public class mnozenie4a {
private static long mnoz_zakres(int n) {
long suma = 0;
for(int x=0; x < n; x++) {
for(int y=0; y < n; y++) {
suma += x * y;
}
}
return suma;
}
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
long wynik = mnoz_zakres(n);
System.out.printf("Wynik: %d\n", wynik);
}
}
import java.util.stream.LongStream;
// W tej wersji używam "lamb i stream-ów", czyli elementów programowania funkcyjnego, dostepnych od Javy 8
public class mnozenie5 {
private static long mnoz_zakres(int n) {
return LongStream.range(0, n)
.flatMap(i -> LongStream.range(0, n).map(j -> i * j))
.sum();
}
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
long wynik = mnoz_zakres(n);
System.out.printf("Wynik: %d\n", wynik);
}
}
import java.util.stream.LongStream;
// W tej wersji używam "parallel stream", aby zrównoleglić obliczenia (wykorzystać wiele procesorów)
public class mnozenie5p {
private static long mnoz_zakres(int n) {
return LongStream.range(0, n)
.parallel()
.flatMap(i -> LongStream.range(0, n)
.parallel()
.map(j -> i * j))
.sum();
}
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
long wynik = mnoz_zakres(n);
System.out.printf("Wynik: %d\n", wynik);
}
}
import java.math.BigInteger;
// W tej wersji do reprezentowania liczb używam klasy BigInteger, czyli liczb bez ograniczenia na wielkość; czegoś, co działa analogicznie do Pythonowego int.
public class mnozenie6 {
private static BigInteger mnoz_zakres(int n) {
BigInteger[] t1 = new BigInteger[n];
BigInteger[] t2 = new BigInteger[n];
for(int i=0; i < n; i++) {
t1[i] = BigInteger.valueOf(i);
t2[i] = BigInteger.valueOf(i);
}
BigInteger suma = BigInteger.ZERO;
for(BigInteger x : t1) {
for(BigInteger y : t2) {
suma = suma.add(x.multiply(y));
}
}
return suma;
}
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
BigInteger wynik = mnoz_zakres(n);
System.out.printf("Wynik: %d\n", wynik);
}
}
import System.Environment
-- Język Haskell. Wersja z tworzeniem tablic.
oblicz::Int -> Int
oblicz n =
let t1 = [0..n-1] in
let t2 = [0..n-1] in
sum([x*y | x <- t1, y <- t2])
main::IO()
main = do
[arg0] <- getArgs
let n = read arg0
let res = oblicz n
print res
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment