{"id":1707,"date":"2010-11-08T18:40:50","date_gmt":"2010-11-08T18:40:50","guid":{"rendered":"http:\/\/www.bartbusschots.ie\/blog\/?p=1707"},"modified":"2010-11-09T20:50:02","modified_gmt":"2010-11-09T20:50:02","slug":"longest-words-followup-java-v-perl","status":"publish","type":"post","link":"https:\/\/www.bartbusschots.ie\/s\/2010\/11\/08\/longest-words-followup-java-v-perl\/","title":{"rendered":"Longest Words Followup &#8211; Java -v- Perl"},"content":{"rendered":"<p>Yesterday I <a href=\"http:\/\/www.bartbusschots.ie\/blog\/?p=1704\">posted<\/a> about using Perl to solve the question &#8220;what&#8217;s the longest word I can type with just half a keyboard?&#8221;. My self an Connor were joking that it would be a lot more difficult with Java, first to write the code, then to run.<\/p>\n<p>I literally used the identical algorithm for the Java program, even using the same variable names, and printed the results out identically (I verified this with the Unix <code>diff<\/code> command). I also did my best to use the various built-in Java functionality and <code>java.util<\/code> classes to minimise the amount of heavy lifting my code had to do.<\/p>\n<p><!--more-->So, this is the resulting code:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nimport java.util.Vector;\nimport java.util.Enumeration;\nimport java.io.BufferedReader;\nimport java.io.File;\nimport java.io.FileReader;\n\npublic class dict{\n    public static void main(String args&#x5B;]){\n        \/\/declare the needed variables\n        String longestLeft=&quot;&quot;, longestRight=&quot;&quot;;\n        int minLength = 10;\n        Vector&lt;String&gt; longLeftWords = new Vector&lt;String&gt;();\n        Vector&lt;String&gt; longRightWords = new Vector&lt;String&gt;();\n        \n        try{\n            \/\/open the dictionary file\n            File file = new File(args&#x5B;0]);\n            BufferedReader reader = null;\n            reader = new BufferedReader(new FileReader(file));\n        \n            \/\/ loop through the file\n            String line;\n            while((line = reader.readLine()) != null){\n                \/\/ remove the trailing new line character from the string\n                line = line.replaceAll(&quot;\\n|\\r&quot;, &quot;&quot;);\n            \n                \/\/ check for characters on the right, if not, then it&#039;s an all-left word\n                if(!line.toLowerCase().matches(&quot;.*&#x5B;yuiophjklnm].*&quot;)){\n                    if(line.length() &gt;= minLength){\n                        longLeftWords.add(line);\n                    }\n                    if(line.length() &gt; longestLeft.length()){\n                        longestLeft = line;\n                    }\n                }\n            \n                \/\/vica-versa\n                if(!line.toLowerCase().matches(&quot;.*&#x5B;qwertasdfgzxcvb].*&quot;)){\n                    if(line.length() &gt;= minLength){\n                        longRightWords.add(line);\n                    }\n                    if(line.length() &gt; longestRight.length()){\n                        longestRight = line;\n                    }\n                }\n            }\n        \n            \/\/ close the dictionary file\n            reader.close();\n        }catch(Exception e){\n            System.out.println(&quot;\\n\\nERROR - Failed to read the dictionary file &#039;&quot; + args&#x5B;0] + &quot;&#039;\\n&quot;);\n            e.printStackTrace();\n            System.exit(1);\n        }\n        \n        \/\/ print the results\n        System.out.println(&quot;\\nLong words (at least &quot; + minLength + &quot; letters) with the left-side of the KB only:&quot;);\n        Enumeration words = longLeftWords.elements();\n        while(words.hasMoreElements()){\n            System.out.println(&quot;\\t&quot; + (String)words.nextElement());\n        }\n        System.out.println(&quot;\\t\\t(total: &quot; + longLeftWords.size() + &quot;)&quot;);\n        System.out.println(&quot;\\nLong words (at least &quot; + minLength + &quot; letters) with the right-side of the KB only:&quot;);\n        words = longRightWords.elements();\n        while(words.hasMoreElements()){\n            System.out.println(&quot;\\t&quot; + (String)words.nextElement());\n        }\n        System.out.println(&quot;\\t\\t(total: &quot; + longRightWords.size() + &quot;)&quot;);\n        System.out.println(&quot;\\nLongest left-only word: &quot; + longestLeft + &quot; (&quot; + longestLeft.length() + &quot; letters)&quot;);\n        System.out.println(&quot;\\nLongest right-only word: &quot; + longestRight + &quot; (&quot; + longestRight.length() + &quot; letters)\\n&quot;);\n    }\n}\n<\/pre>\n<p>The obvious thing is that it&#8217;s longer than yesterday&#8217;s final delux Perl solution, about twice as long in fact. The code is also much wordier, with the lines being longer than in the Perl version. There&#8217;s also a heck of a lot of &#8216;fluff&#8217; in Java. In perl it literally takes two characters (<code>&lt;&gt;<\/code>), while in Java it takes about 6 when you include the mandatory exception handling. Getting a variable-length array is also far more cumbersome, using <code>java.util.Vector<\/code> helps a lot, but it means you have to use <code>java.util.Enumeration<\/code> to iterate through your vector for printing instead of a simple foreach loop like in Perl. Finally, notice how much clunkier the regular expressions are! Nothing as trivial as the <code>m<\/code> operator in Perl in Java<\/p>\n<p>OK, so the code is longer, more fluffy, and harder to read and write, but how does it run? The simple answer, slower! About three times slower in fact:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nbartmbp:Temp bart$ time .\/dict.pl \/usr\/share\/dict\/words &gt;&gt;\/dev\/null\n\nreal\t0m0.761s\nuser\t0m0.275s\nsys\t0m0.010s\nbartmbp:Temp bart$ time java dict \/usr\/share\/dict\/words &gt;&gt;\/dev\/null\n\nreal\t0m2.391s\nuser\t0m2.230s\nsys\t0m0.121s\nbartmbp:Temp bart$\n<\/pre>\n<p>Given that Perl is a scripting language and Java is at least partially compiled, you&#8217;d expect Java to have the edge. But, when it comes to pattern matching, Perl is in its element, while Java is really rather lost. I think it&#8217;s Java&#8217;s poor RE engine that&#8217;s making the difference here.<\/p>\n<p>So, there you have it, Perl really is quicker and simpler for messing with text. Who knew \ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday I posted about using Perl to solve the question &#8220;what&#8217;s the longest word I can type with just half a keyboard?&#8221;. My self an Connor were joking that it would be a lot more difficult with Java, first to write the code, then to run. I literally used the identical algorithm for the Java [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_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_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1,12],"tags":[],"series":[],"class_list":["post-1707","post","type-post","status-publish","format-standard","hentry","category-uncategorized","category-computers-tech"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p7t9xK-rx","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/posts\/1707","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/comments?post=1707"}],"version-history":[{"count":4,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/posts\/1707\/revisions"}],"predecessor-version":[{"id":1711,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/posts\/1707\/revisions\/1711"}],"wp:attachment":[{"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/media?parent=1707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/categories?post=1707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/tags?post=1707"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.bartbusschots.ie\/s\/wp-json\/wp\/v2\/series?post=1707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}