# Is there a way to speed up this code?

**URL:** <https://discourse.julialang.org/t/is-there-a-way-to-speed-up-this-code/102878>\
**Category:** Performance\
**Tags:** question, parsing, io\
**Created:** [August 16, 2023, 7:11pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-speed-up-this-code/102878 "2023-08-16T19:11:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luis\_Manuel\_Espinoza](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luis_manuel_espinoza/32/36591_2.png) [@Luis\_Manuel\_Espinoza](https://discourse.julialang.org/u/Luis_Manuel_Espinoza)\
**Post date:** [August 16, 2023, 7:11pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-speed-up-this-code/102878/1 "2023-08-16T19:11:43Z")

</div>

function open\_xyz\_file(path::AbstractString)  
file = open(path)  
num\_atoms = parse(Int, readline(file))  
comment = readline(file)  
symbols = Vector{String}(undef, num\_atoms)  
coords = zeros(num\_atoms,3)  
for i in 1:num\_atoms  
line = readline(file)  
data = split(line)  
symbols[i] = data[1]  
coords[i, :] = [parse(Float64, d) for d in data[2:4]]  
end  
close(file)  
return symbols, coords, comment  
end

---

<div class="post-metadata">

**Author:** ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)\
**Post date:** [August 16, 2023, 7:26pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-speed-up-this-code/102878/2 "2023-08-16T19:26:38Z")

</div>

Hello @Luis_Manuel_Espinoza ,

You would make it easier for anybody willing to help if you would take a look at the following [guidelines](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757) and provide a nicer code formatting.

One quick improvement can be the removal of allocations associated with: `coords[i, :] = [parse(Float64, d) for d in data[2:4]]` line.

A potential replacement is this:

```julia
for ii in 1:3
    coords[i, ii] = parse(Float64, data[ii+1])
end 

```

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [August 16, 2023, 7:27pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-speed-up-this-code/102878/3 "2023-08-16T19:27:02Z")

</div>

1. Consider reading the file all at once instead of line-by-line.
2. It is better to use static arrays for small vectors
3. `data[2:4]` makes a copy
4. `[parse(Float64, d) for d in data[2:4]]` allocates a temporary array

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 16, 2023, 9:26pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-speed-up-this-code/102878/4 "2023-08-16T21:26:44Z")

</div>

> [@Jeff\_Emanuel](#):
>
> Consider reading the file all at once instead of line-by-line.

There are ways to do this faster: [Package to read/process lines without new allocations](https://discourse.julialang.org/t/package-to-read-process-lines-without-new-allocations/92448)

But for this specific task of reading a sequence of lines containing space-delimited numbers that you want to parse, I would look at [`CSV.Rows` in CSV.jl](https://csv.juliadata.org/stable/reading.html#CSV.Rows), or simply [`CSV.File`](https://csv.juliadata.org/stable/reading.html#CSV.File) with a `limit=num_atoms` argument.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [August 16, 2023, 10:09pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-speed-up-this-code/102878/5 "2023-08-16T22:09:02Z")

</div>

There are also a few libraries to deal with common atom file formats, for instance: [Julia interface to chemfiles · Chemfiles.jl](https://chemfiles.org/Chemfiles.jl/latest/)

From their examples: [Chemfiles — read and write chemistry files](https://chemfiles.org/#julia-example)

```julia
using Chemfiles

file = Trajectory("filename.xyz")
frame = read(file)

println("There are $(size(frame)) atoms in the frame")
pos = positions(frame);

# Do awesome science here with the positions

if has_velocities(frame)
    vel = velocities(frame)

    # If the file contains information about the
    # velocities, you will find them here.
end

```
