source code migrating from gitlab
This commit is contained in:
@@ -0,0 +1,32 @@
|
|||||||
|
# The Benchmarks
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## Info
|
||||||
|
|
||||||
|
If you want to measure the time and memory you can use the `time` function. In `zsh` and `bash` (maybe others) there is a built in `time` but you probably have one at `/usr/sbin/time`
|
||||||
|
|
||||||
|
|
||||||
|
Here is the `TIMEFMT` I used to display the stats
|
||||||
|
|
||||||
|
```bash
|
||||||
|
export TIMEFMT='%J'$'\n'\
|
||||||
|
'user %U'$'\n'\
|
||||||
|
'system %S'$'\n'\
|
||||||
|
'total %E'$'\n'\
|
||||||
|
'cpu %P'$'\n'\
|
||||||
|
'max memory: %M KB'$'\n'
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Each directory contains a `Makefile` and the `build.sh` and `clean.sh` just go into each directory and call `make` and `make clean`
|
||||||
|
|
||||||
|
## Links
|
||||||
|
|
||||||
|
This is the GitHub repo for the Speed Tests [repo](https://github.com/jabbalaci/SpeedTests)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
You can watch this video [here](https://youtu.be/Akfr_dcgZE8)
|
||||||
|
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
pushd c-lang
|
||||||
|
make
|
||||||
|
popd
|
||||||
|
|
||||||
|
pushd c3-lang
|
||||||
|
make
|
||||||
|
popd
|
||||||
|
|
||||||
|
pushd odin-lang
|
||||||
|
make
|
||||||
|
popd
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
all:
|
||||||
|
clang main.c -o main -lm
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm main
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
#include <ctype.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
unsigned int getSum(FILE *file);
|
||||||
|
char getFirstDigit(const char *line, size_t len);
|
||||||
|
char getLastDigit(const char *line, size_t len);
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
|
if (argc != 2) {
|
||||||
|
printf("error: missing file input\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
FILE *file = fopen(argv[1], "r");
|
||||||
|
if (file == NULL) {
|
||||||
|
printf("could not open file: %s\n", argv[1]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int sum = getSum(file);
|
||||||
|
printf("%d\n", sum);
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int getSum(FILE *file) {
|
||||||
|
|
||||||
|
unsigned int sum = 0;
|
||||||
|
size_t len = 0;
|
||||||
|
size_t read;
|
||||||
|
char *line = NULL;
|
||||||
|
|
||||||
|
while ((read = getline(&line, &len, file)) != -1) {
|
||||||
|
char first = getFirstDigit(line, read);
|
||||||
|
char last = getLastDigit(line, read);
|
||||||
|
|
||||||
|
char digits[3] = {first, last, '\0'};
|
||||||
|
sum += atoi(digits);
|
||||||
|
}
|
||||||
|
|
||||||
|
// free(line);
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
char getFirstDigit(const char *line, size_t len) {
|
||||||
|
|
||||||
|
char digit;
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
if (isdigit(line[i])) {
|
||||||
|
digit = (char)line[i];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return digit;
|
||||||
|
}
|
||||||
|
|
||||||
|
char getLastDigit(const char *line, size_t len) {
|
||||||
|
|
||||||
|
char digit;
|
||||||
|
for (int i = len - 1; i >= 0; i--) {
|
||||||
|
if (isdigit(line[i])) {
|
||||||
|
digit = (char)line[i];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return digit;
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
all:
|
||||||
|
c3c compile main.c3
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm main
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
module main;
|
||||||
|
import std::io;
|
||||||
|
|
||||||
|
fn int main(String[] args) {
|
||||||
|
|
||||||
|
if (args.len < 2) {
|
||||||
|
io::printn("Usage: HelloWorld <filename>");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
String filename = args[1];
|
||||||
|
|
||||||
|
int? answer = getSum(filename);
|
||||||
|
if (catch error = answer) {
|
||||||
|
io::printfn("error opening file: %s", error);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
io::printfn("%d", answer);
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn int? getSum(String filename) {
|
||||||
|
File? file = file::open(filename, "r")!;
|
||||||
|
|
||||||
|
defer (void)file.close();
|
||||||
|
|
||||||
|
int sum = 0;
|
||||||
|
|
||||||
|
while (try line = io::treadline(&file)) {
|
||||||
|
char first = getFirst(line);
|
||||||
|
char last = getLast(line);
|
||||||
|
String digits = {first, last};
|
||||||
|
int? number = digits.to_int();
|
||||||
|
if (catch error = number) {
|
||||||
|
io::printfn("Error converting to number: %s", error);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
sum += number;
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn char getLast(String line) {
|
||||||
|
char last;
|
||||||
|
int length = line.len - 1;
|
||||||
|
for (int i=length; i>=0; i--) {
|
||||||
|
if (ascii::is_digit(line[i])) {
|
||||||
|
last = line[i];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return last;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn char getFirst(String line) {
|
||||||
|
char first;
|
||||||
|
foreach (ch : line) {
|
||||||
|
if (ascii::is_digit(ch)) {
|
||||||
|
first = ch;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return first;
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
pushd c-lang
|
||||||
|
make clean
|
||||||
|
popd
|
||||||
|
|
||||||
|
pushd c3-lang
|
||||||
|
make clean
|
||||||
|
popd
|
||||||
|
|
||||||
|
pushd odin-lang
|
||||||
|
make clean
|
||||||
|
popd
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
all:
|
||||||
|
odin build . -out:main
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm main
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "core:fmt"
|
||||||
|
import "core:os"
|
||||||
|
import "core:strconv"
|
||||||
|
import "core:strings"
|
||||||
|
import "core:unicode/utf8"
|
||||||
|
|
||||||
|
main :: proc() {
|
||||||
|
|
||||||
|
if len(os.args) < 2 {
|
||||||
|
fmt.println("Error: file required")
|
||||||
|
os.exit(-1)
|
||||||
|
}
|
||||||
|
filename := os.args[1]
|
||||||
|
|
||||||
|
sum, ok := getSum(filename)
|
||||||
|
if !ok {
|
||||||
|
os.exit(-1)
|
||||||
|
}
|
||||||
|
fmt.println(sum)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
getSum :: proc(filename: string) -> (int, bool) {
|
||||||
|
data, err := os.read_entire_file(filename, context.allocator)
|
||||||
|
if err != nil {
|
||||||
|
fmt.println("error opening file", err)
|
||||||
|
return -1, false
|
||||||
|
}
|
||||||
|
defer delete(data, context.allocator)
|
||||||
|
sum := 0
|
||||||
|
|
||||||
|
iter := string(data)
|
||||||
|
|
||||||
|
for line in strings.split_lines_iterator(&iter) {
|
||||||
|
first := getFirst(line)
|
||||||
|
last := getLast(line)
|
||||||
|
|
||||||
|
// convert to rune array
|
||||||
|
digits := []rune{first, last}
|
||||||
|
// convert rune array to string first?
|
||||||
|
str := utf8.runes_to_string(digits, context.allocator)
|
||||||
|
// covert string to int..
|
||||||
|
val, ok := strconv.parse_int(str)
|
||||||
|
if !ok {
|
||||||
|
fmt.println("error converting to int")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
sum += val
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return sum, true
|
||||||
|
}
|
||||||
|
|
||||||
|
getFirst :: proc(line: string) -> rune {
|
||||||
|
first: rune
|
||||||
|
|
||||||
|
for ch in line {
|
||||||
|
if ch >= '0' && ch <= '9' {
|
||||||
|
first = ch
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return first
|
||||||
|
}
|
||||||
|
|
||||||
|
getLast :: proc(line: string) -> rune {
|
||||||
|
last: rune
|
||||||
|
|
||||||
|
length := len(line) - 1
|
||||||
|
for i := length; i >= 0; i -= 1 {
|
||||||
|
if line[i] >= '0' && line[i] <= '9' {
|
||||||
|
last = rune(line[i])
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return last
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user