Appendix B Solutions to exercises
Exercise 3.1
Create an x variable that is a range of values from 1 to 10^6. Now, using the collect function, create a y vector holding the same values as the x range. Using the @btime macro, check the time of sorting x and y by using the sort function. Finally, using the @edit macro, check the implementation of the sort function that would be invoked when you sort the x range.
Solution
julia> using BenchmarkTools julia> x = 1:10^6; julia> y = collect(x); julia> @btime sort($x); 1.100 ns (0 allocations: 0 bytes) julia> @btime sort($y); 7.107 ms (2 allocations: 7.63 MiB) julia> @edit sort(x)
Observe that sorting the x range is much faster than sorting the y vector. If you have a properly configured Julia environment ...
Get Julia for Data Analysis now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.