Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most efficient way of writing data to a csv file with Julia

Tags:

julia

I want to write large amounts of data in Julia language. The data is generated and then stored in lists. A pseudocode is:

f = open("test.csv", "w")

for i = 1:3
    position = [j for j = i:(i + 10) ]
    string_position = string(position)
    n = length(string_position)
    write(f, string_position[2:(n - 1)]*"\n")
end

close(f)

However it seems inefficient to get the length of the strings in each iteration and then remove the first and last element of the string.

Is there a faster way?

like image 665
RM- Avatar asked Aug 25 '16 20:08

RM-


People also ask

How do I write to a file in Julia?

Writing To a File In Julia In order to write in a file, we need to open the file in write mode by passing “w” as a parameter. Now to write to a file in Julia we use write(fileobject, string) method. It takes two arguments, first is the file object and the second is the String provided.

Are CSV files efficient?

Well, yes, but no — it's a highly efficient data storage format, and you'll learn all about it today. CSVs are everywhere — from company reports to machine learning datasets. It's a data format that's simple and intuitive to work with — just open a file, and you have direct access to the data.


1 Answers

One simple optimization is to use

write(f, string_position[2:(n - 1)], "\n")

instead of *. This writes the two objects in succession, instead of first concatenating them and then writing the result.

It might also be faster to use a SubString, which references part of another string in place without copying.

In general, it is also likely to be faster to avoid creating intermediate strings. Instead of first making a string and then writing it, write the individual items. For example

for item in position
    print(f, item, ",")
end
print(f, "\n")

I should add that there is a writecsv function in the standard library that does this for you.

like image 95
Jeff Bezanson Avatar answered Oct 05 '22 07:10

Jeff Bezanson