{"id":2231,"date":"2025-05-08T12:34:15","date_gmt":"2025-05-08T16:34:15","guid":{"rendered":"https:\/\/www.unliterate.net\/?p=2231"},"modified":"2025-05-09T12:40:14","modified_gmt":"2025-05-09T16:40:14","slug":"learning-python","status":"publish","type":"post","link":"https:\/\/www.unliterate.net\/index.php\/2025\/05\/08\/learning-python\/","title":{"rendered":"Learning python"},"content":{"rendered":"\n<p>Part of learning a new programming language when you already know a couple is getting deep into the nitty gritty of it. With Python being the new <a href=\"https:\/\/en.wikipedia.org\/wiki\/BASIC\" target=\"_blank\" rel=\"noreferrer noopener\">BASIC<\/a> I decided to start with something, well, basic.<\/p>\n\n\n\n<p>I remember &#8220;back in the good old days&#8221; when there was a goofy standard of &#8220;web-safe colors&#8221;. Specifically there were <a href=\"https:\/\/en.wikipedia.org\/wiki\/Web_colors#Web-safe_colors\" target=\"_blank\" rel=\"noreferrer noopener\">216 defined colors<\/a>, so I decided to start by writing a program to take an image and covert it as if it was presentable in the late 1990s.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>This exercise requires knowledge of storing a list of three pieces of information, performing mathematic computations to find a nearest color, and using libraries to load and save images. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">PHP<\/h2>\n\n\n\n<p>source: <a href=\"https:\/\/github.com\/mjheick\/web-safe-colors\/blob\/master\/web-safe-colors.php\" target=\"_blank\" rel=\"noreferrer noopener\">web-safe-colors.php<\/a><\/p>\n\n\n\n<p>PHP makes this easy with GD and faster with JIT built into 8.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python<\/h2>\n\n\n\n<p>source: <a href=\"https:\/\/github.com\/mjheick\/web-safe-colors\/blob\/master\/web-safe-colors.py\" target=\"_blank\" rel=\"noreferrer noopener\">web-safe-colors.py<\/a><\/p>\n\n\n\n<p>Python made this easy as well with PIL, the right Math libraries, and how easy array elements are to access.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Execution<\/h2>\n\n\n\n<p>I noticed a significant delay when running the Python version over the PHP version and decided to benchmark it over 10 executions:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for x in `seq 1 10`; do time php -f web-safe-colors.php src.jpg; done;\nfor x in `seq 1 10`; do time python web-safe-colors.py src.jpg; done; <\/code><\/pre>\n\n\n\n<p><strong>PHP:<\/strong> 1m8.973s, 1m9.619s, 1m8.960s, 1m8.981s, 1m9.235s, 1m8.967s, 1m9.447s, 1m8.888s, 1m8.985s, 1m9.472s. <strong>Average 1m9.1527<\/strong><br><strong>Python:<\/strong>  9m25.760s, 9m29.751s, 9m11.790s, 9m24.404s, 9m20.062s, 9m29.795s, 9m33.445s, 9m22.530s, 9m12.430s, 9m25.237s. <strong>Average 9m23.5204<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Lesson<\/h2>\n\n\n\n<p>Learning python is the current norms &#8220;BASIC&#8221;, just like my middle schools&#8217; Turtle Graphics, and it&#8217;s exactly what it needs to be: Easy to use, easy to maintain, easy to hire people. The performance, however, isn&#8217;t blowing my hair away.<\/p>\n\n\n\n<p>Due to my googling and watching instructional videos a <a href=\"https:\/\/www.youtube.com\/shorts\/JvMh6yshgFo\" target=\"_blank\" rel=\"noreferrer noopener\">random short<\/a> came my way which had an interesting comment that summarizes the performance point perfectly: &#8220;never be worried about performance in python&#8221; &#8211; because if you&#8217;re worried about performance, you shouldn&#8217;t be using python.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Part of learning a new programming language when you already know a couple is getting deep into the nitty gritty of it. With Python being the new BASIC I decided to start with something, well, basic. I remember &#8220;back in the good old days&#8221; when there was a goofy standard of &#8220;web-safe colors&#8221;. Specifically there [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,6],"tags":[],"class_list":["post-2231","post","type-post","status-publish","format-standard","hentry","category-geek-instructions","category-youtube"],"_links":{"self":[{"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/posts\/2231","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/comments?post=2231"}],"version-history":[{"count":5,"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/posts\/2231\/revisions"}],"predecessor-version":[{"id":2236,"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/posts\/2231\/revisions\/2236"}],"wp:attachment":[{"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/media?parent=2231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/categories?post=2231"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.unliterate.net\/index.php\/wp-json\/wp\/v2\/tags?post=2231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}