Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4897

Teaching and learning resources • Re: Advent of Code 2024

$
0
0

Fido opened one eye and typed, "Is there any way to performance optimize the above code?"
The statement

Code:

  var lines = file.readlines();
appears to be a hallucination.
It looks more paradigmatic to me. Unfortunately, the run time

Code:

$ ./day01 -nl 1Advent of Code 2024 Day 01 Historian HysteriaPart 1 The total distance between lists is 2000468Part 2 The similarity between lists is 18567089Total execution time 0.006961 seconds.
indicates the execution speed is

(0.006961-0.005264)/0.005264 = 32 percent

slower than before.
The first Copilot suggestion--reading a list and then converting the list to an array--seems better. With

Code:

proc doinit(){    var io=open("day01.txt",ioMode.r);    var fp=io.reader(locking=false);    var data=fp.lines();    var idxl,idyl:list(int);    var k=0;    for s in fp.lines() {        k+=1;        var v=s.split();        idxl.pushBack(v(0):int);        idyl.pushBack(v(1):int);    }    var idx,idy:[1..k]int;    idx=idxl; idy=idyl;    var p1=part1(idx,idy);    writef("Part 1 The total distance between lists is %i\n",p1);    var p2=part2(idx,idy);    writef("Part 2 The similarity between lists is %i\n",p2);}   
Note pushBack is used to add an element to the end of a list since append seems to be another hallucination.

The result

Code:

$ ./day01 -nl 1Advent of Code 2024 Day 01 Historian HysteriaPart 1 The total distance between lists is 0Part 2 The similarity between lists is 0Total execution time 0.002271 seconds.
indicates a

(0.005264-0.002271)/0.005264 = 58 percent

speedup over the original Chapel code.

Statistics: Posted by ejolson — Tue Dec 03, 2024 8:17 pm



Viewing all articles
Browse latest Browse all 4897

Trending Articles