diff options
-rw-r--r-- | 2020/rust/Cargo.toml | 1 | ||||
-rw-r--r-- | 2020/rust/day01/Cargo.toml | 1 | ||||
-rw-r--r-- | 2020/rust/day01/src/main.rs | 36 | ||||
-rwxr-xr-x | bin/measure_mem.sh | 64 | ||||
-rwxr-xr-x | bin/measure_time.sh | 40 |
5 files changed, 127 insertions, 15 deletions
diff --git a/2020/rust/Cargo.toml b/2020/rust/Cargo.toml index 9674b94..ee71f9e 100644 --- a/2020/rust/Cargo.toml +++ b/2020/rust/Cargo.toml @@ -1,5 +1,6 @@ [workspace] members = [ + "aoc", "day01", # "day02", # "day03", diff --git a/2020/rust/day01/Cargo.toml b/2020/rust/day01/Cargo.toml index 674217d..4bf07f8 100644 --- a/2020/rust/day01/Cargo.toml +++ b/2020/rust/day01/Cargo.toml @@ -5,4 +5,5 @@ authors = ["cos <cos>"] edition = "2018" [dependencies] +aoc = { path = "../aoc" } anyhow = "1.0" diff --git a/2020/rust/day01/src/main.rs b/2020/rust/day01/src/main.rs index 38f25d2..fb661e6 100644 --- a/2020/rust/day01/src/main.rs +++ b/2020/rust/day01/src/main.rs @@ -43,6 +43,8 @@ fn part2(input: &[usize]) -> Option<usize> { } fn main() { + let ( do_part_1, do_part_2 ) = aoc::do_parts(); + let filename = match args().nth(1) { Some(f) => f, None => { @@ -50,22 +52,26 @@ fn main() { std::process::exit(1); }, }; - match read_input(filename) { + match read_input(filename) { Ok(input) => { - match part1(&input) { - Some(solution) => println!("Part1, product found to be: {}", solution), - None => { - eprintln!("Part1, no solution found"); - std::process::exit(1); - } - }; - match part2(&input) { - Some(solution) => println!("Part2, product found to be: {}", solution), - None => { - eprintln!("Part2, no solution found"); - std::process::exit(1); - } - }; + if do_part_1 { + match part1(&input) { + Some(solution) => println!("Part1, product found to be: {}", solution), + None => { + eprintln!("Part1, no solution found"); + std::process::exit(1); + } + }; + } + if do_part_2 { + match part2(&input) { + Some(solution) => println!("Part2, product found to be: {}", solution), + None => { + eprintln!("Part2, no solution found"); + std::process::exit(1); + } + }; + } }, Err(err) => eprintln!("Could not read input: {}", err), } diff --git a/bin/measure_mem.sh b/bin/measure_mem.sh new file mode 100755 index 0000000..9839815 --- /dev/null +++ b/bin/measure_mem.sh @@ -0,0 +1,64 @@ +#!/bin/sh -e + +YEAR="${YEAR:-$(date +%Y)}" +DAY="${DAY:-$(TZ=EST date '+%d')}" +IMPL="${IMPL:-rust}" +PARTS="${AOC_PARTS:-1 2}" + +ITERS="${ITERS:-10}" +MODE="${MODE:-valgrind}" + +cd "${YEAR}/${IMPL}/day${DAY}" +case "${IMPL}" in + 'rust') + cargo build --release + EXECUTABLE="../target/release/day${DAY}" + ;; + *) + echo "Unknown implementation: ${IMPL}" >&2 + exit + ;; +esac + +# https://stackoverflow.com/q/774556/peak-memory-usage-of-a--process +for PART in $(echo ${PARTS}) +do + export AOC_PARTS="${PART}" + + OUT="memmax-${YEAR}-${IMPL}-${DAY}-${PART}.txt" + case "${MODE}" in + 'valgrind') + valgrind --tool=massif --pages-as-heap=yes \ + --massif-out-file=massif.out \ + "${EXECUTABLE}" "${@}" + grep mem_heap_B massif.out | \ + sed -e 's/mem_heap_B=\(.*\)/\1/' | sort -g | tail -n 1 \ + >"${OUT}" + MEM=$(cat "${OUT}") + printf "${YEAR}-${IMPL}-${DAY}-${PART}: MEM: ${MEM} %dk %dM\n" \ + $(( MEM / 1024 )) $(( MEM / 1024 / 1024 )) + ;; + 'time') + # %X The average amount in (shared) text space used in kilobytes. + # %D The average amount in (unshared) data/stack space used in kilobytes. + # %K The total space used (%X+%D) in kilobytes. + # %M The maximum memory the process had in use at any time in kilobytes. + export TIMEFMT='%M' + for I in $(seq "${ITERS}"); do + zsh -c time "${EXECUTABLE}" "${@}" 2>&1 >/dev/null | tail -1 + done >"${OUT}" + + ALL_MEMORY_USAGES="$(cat "${OUT}" )" + NO_OF_ITERS=$(echo "${ALL_MEMORY_USAGES}" | wc -l) + ARITHM="$(echo "( ${ALL_MEMORY_USAGES}" | tr '\n' '+'; \ + echo "0 ) / ${NO_OF_ITERS}" )" + MEAN_MEMORY=$(( $(echo ${ARITHM}) )) + + echo "${YEAR}-${IMPL}-${DAY}-${PART}: Mean: ${MEAN_MEMORY}kB" + echo "${YEAR}-${IMPL}-${DAY}-${PART}: Mean: ${MEAN_MEMORY}kB" >>"${OUT}" + ;; + *) + echo "Unkown mode ${MODE}" >&2 + ;; + esac +done diff --git a/bin/measure_time.sh b/bin/measure_time.sh new file mode 100755 index 0000000..d308b19 --- /dev/null +++ b/bin/measure_time.sh @@ -0,0 +1,40 @@ +#!/bin/sh -e + +YEAR="${YEAR:-$(date +%Y)}" +DAY="${DAY:-$(TZ=EST date '+%d')}" +IMPL="${IMPL:-rust}" +PARTS="${AOC_PARTS:-1 2}" + +ITERS="${ITERS:-10}" + +cd "${YEAR}/${IMPL}/day${DAY}" +case "${IMPL}" in + 'rust') + cargo build --release + EXECUTABLE="../target/release/day${DAY}" + ;; + *) + echo "Unknown implementation: ${IMPL}" >&2 + exit + ;; +esac + +for PART in $(echo ${PARTS}) +do + export AOC_PARTS="${PART}" + OUT="time-${YEAR}-${IMPL}-${DAY}-${PART}.txt" + + export TIMEFMT='%uE' + for I in $(seq "${ITERS}"); do + zsh -c time "${EXECUTABLE}" "${@}" 2>&1 >/dev/null | tail -1 + done >"${OUT}" + + ALL_TIMINGS="$(cat "${OUT}" | sed 's/us/ + /' )" + NO_OF_ITERS=$(echo "${ALL_TIMINGS}" | wc -l) + ARITHM="$(echo "( ${ALL_TIMINGS}" | tr -d '\n'; \ + echo "0 ) / ${NO_OF_ITERS}" )" + MEAN_TIMING=$(( $(echo ${ARITHM}) )) + + printf "${YEAR}-${IMPL}-${DAY}-${PART}: Mean: ${MEAN_TIMING}µs\n" + printf "${YEAR}-${IMPL}-${DAY}-${PART}: Mean: ${MEAN_TIMING}µs\n" >>"${OUT}" +done |