{"id":840,"date":"2021-12-16T17:01:31","date_gmt":"2021-12-16T15:01:31","guid":{"rendered":"https:\/\/www.juustila.com\/antti\/?p=840"},"modified":"2021-12-17T10:26:25","modified_gmt":"2021-12-17T08:26:25","slug":"java-file-management-hall-of-fame-and-a-nice-surprise","status":"publish","type":"post","link":"https:\/\/www.juustila.com\/antti\/2021\/12\/16\/java-file-management-hall-of-fame-and-a-nice-surprise\/","title":{"rendered":"Java file management, hall of fame and a nice surprise"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In the previous post I mocked the Java app that was hardcoded to use Windows C: disk as the default place to open files.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What then is the recommended way? One is to start looking from the user home directory:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>JFileChooser fileChooser = new JFileChooser();\n\nfileChooser.setCurrentDirectory(new File(System.getProperty(\"user.home\")));<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Or pick the documents directory. It is also a nice thing to save the selected directory and use that if the user would like to continue with the already selected directory next time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What else is happening? It is now the last week of the course Data Structures and Algorithms I am responsible teacher. Students have been analyzing algorithm correctness and time complexity, implementing basic data structures, more advanced ones like hash tables and hash functions, binary search trees and binary search. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Lectures addressed graphs and graph algorithms too, but implementation of these was in an optional course project only, Mazes. When students finish that, they can <a href=\"https:\/\/youtube.com\/watch?v=z0_fUorcKrU&amp;feature=share\">play a game a bit like the PacMan<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">They get to choose from two course projects: either that Mazes project or optionally implement the classic task: count the unique words from a book file, ignoring some words from another file. The largest test file is around 16 MB, having almost 100 000 unique words. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Processing the largest file, using naively two nested for loops takes on my Mac Mini M1 with 16MB of memory around two minutes to process. The fast versions (hash tables, binary search trees) take less than a second.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I have implemented several different solutions for comparison with three different programming languages, Java, <a href=\"https:\/\/github.com\/anttijuu\/BooksAndWords\">Swift<\/a> and C++. Each week in lectures I demonstrated some of these, and in the end we had this table for comparison (sorry, in Finnish only).<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"519\" src=\"https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/12\/Nayttokuva-2021-12-16-kello-16.41.05-1024x519.png\" alt=\"\" class=\"wp-image-841\" srcset=\"https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/12\/Nayttokuva-2021-12-16-kello-16.41.05-1024x519.png 1024w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/12\/Nayttokuva-2021-12-16-kello-16.41.05-300x152.png 300w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/12\/Nayttokuva-2021-12-16-kello-16.41.05-768x389.png 768w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/12\/Nayttokuva-2021-12-16-kello-16.41.05-1536x779.png 1536w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/12\/Nayttokuva-2021-12-16-kello-16.41.05-535x271.png 535w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/12\/Nayttokuva-2021-12-16-kello-16.41.05-297x150.png 297w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/12\/Nayttokuva-2021-12-16-kello-16.41.05.png 1694w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><figcaption> Hall of Fame of the different Books and Words implementations. Swift versions can be found from the link above to GitHub.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">As you can see, the C++ with 8 threads was the fastest one. Next after C++ came a couple of Java versions. Swift implementations were not so fast as I expected. After some profiling, I suspect the reason is in the way Unicode chars are handled in Swift. All the book  files are UTF-8 and students were expected to handle them correctly. I do not like that mostly in teaching programming languages the default is to stick with ascii and conveniently forget the existence of different languages and character sets.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Well, anyways, for some reason, processing these UTF-8 text files takes a lot of time with Swift. Maybe later I have time to find out if the issue is in my code and\/or is there anything that can be done to speed things up.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Something very nice happened a week ago &#8212; the student guild of our study program, <a rel=\"noreferrer noopener\" href=\"https:\/\/blanko.fi\" target=\"_blank\">Blanko<\/a>, awarded me this diploma for being a quality teacher. Apparently they had a vote and I somehow managed to come first this time. The diploma was accompanied by this Italian themed small gift box. A really nice surprise! I was so astonished to receive this, thank you so much if anyone of you is reading!<\/p>\n\n\n\n<div class=\"wp-block-image is-style-rounded\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/12\/Blanko-576x1024.jpg\" alt=\"\" class=\"wp-image-842\" width=\"432\" height=\"768\" srcset=\"https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/12\/Blanko-576x1024.jpg 576w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/12\/Blanko-169x300.jpg 169w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/12\/Blanko-768x1365.jpg 768w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/12\/Blanko-864x1536.jpg 864w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/12\/Blanko-1152x2048.jpg 1152w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/12\/Blanko-535x951.jpg 535w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2021\/12\/Blanko-scaled.jpg 1440w\" sizes=\"auto, (max-width: 432px) 85vw, 432px\" \/><figcaption>Nice award from the students for quality teaching.<\/figcaption><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In the previous post I mocked the Java app that was hardcoded to use Windows C: disk as the default place to open files. What then is the recommended way? One is to start looking from the user home directory: Or pick the documents directory. It is also a nice thing to save the selected &hellip; <a href=\"https:\/\/www.juustila.com\/antti\/2021\/12\/16\/java-file-management-hall-of-fame-and-a-nice-surprise\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Java file management, hall of fame and a nice surprise&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[2],"tags":[48,10,11,77,39,8,46,12,17],"class_list":["post-840","post","type-post","status-publish","format-standard","hentry","category-coding","tag-algorithms","tag-c","tag-cstd","tag-java","tag-performance","tag-stdthread","tag-swift","tag-teaching","tag-threads"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts\/840","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/comments?post=840"}],"version-history":[{"count":6,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts\/840\/revisions"}],"predecessor-version":[{"id":848,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts\/840\/revisions\/848"}],"wp:attachment":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/media?parent=840"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/categories?post=840"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/tags?post=840"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}