Basti's Scratchpad on the Internet
10 Apr 2018

Appending to Matlab Arrays

The variable $var appears to change size on every loop iteration. Consider preallocating for speed.

So sayeth Matlab.

Let's try it:

x_prealloc = cell(10000, 1);
x_end = {};
x_append = {};
for n=1:10000
    % variant 1: preallocate
    x_prealloc(n) = {42};
    % variant 2: end+1
    x_end(end+1) = {42};
    % variant 3: append
    x_append = [x_append {42}];
end

Which variant do you think is fastest?

array_performance.png

Unsurprisingly, preallocation is indeed faster than growing an array. What is surprising is that it is faster by a constant factor of about 2 instead of scaling with the array length. Only appending by x = [x {42}] actually becomes slower for larger arrays. (The same thing happens for numerical arrays, struct arrays, and object arrays.)

TL;DR: Do not use x = [x $something], ever. Instead, use x(end+1) = $something. Preallocation is generally overrated.

Tags: matlab
07 Apr 2018

Defeating News Addiction

I want to consume the news, both because it is genuinely relevant for my work, and because conversations about news are part of my social life. But I do not want to be consumed by news, and end up scanning news websites over and over for new content, even though you know that the likelihood of finding anything interesting is small.

Over the last few months, I have tried hard to find all instances of this repeated-scanning behavior, and eliminate it. The key is to automate the scanning such that I am only ever presented with new content, but do not get hooked on the addictive variable-reward cycle of checking websites for changes over and over again.

And it all works thank to the magic of RSS:

With all this settled, I have a veritable firehose of news every day. I estimate that only 1 % of this is actually worth reading. So in the next step, I filter this list for spam. For this purpose, I use Feedbin, which aggegates all these feeds, and remembers whether I have read an article. The remaining ham I either read immediately, or forward it to Pinboard for later consumption.

With this system, I never miss anything, but once I consume all the news in my feed reader, I know I am done, and there is no point in checking and re-checking various websites over and over again.

ACHIEVEMENT UNLOCKED Defeated News Addiction!
Tags: workflow
02 Apr 2018

Scheduling Future TODOs in org-journal

I keep a simple journal in org-journal: One text file per day, in org-mode. But over the years, org-journal has grown somewhat beyond this simple use case. About three years ago, a gentleman named Vladimir Kazanov implemented a very fast text search. Thus, my journal became an information archive. About two years ago, org-journal learned to carry over TODO items to the current day if you hadn't completed them on the previous day. So it to become a to-do list. And today, org-journal gained the ability to work with future journal entries, thus becoming a calendar.

Despite all of these features however, org-journal remains one org file per day, with fancy functions to do fancy things if you want them—or ignore them, if the journal is all you need.

Back to scheduling: This work was prompted by my colleague, who organizes everything in org-mode, but is not a user of org-journal. He even eschews the use of a traditional calendar, and instead uses a few org files and the magic of org-agenda to give him a nice overview like this for the coming week1:

Week-agenda (W14):
Monday     2 April 2018 W14
  2018-04-02: Easter Monday 
Tuesday    3 April 2018
Wednesday  4 April 2018
Thursday   5 April 2018
  2018-04-05: Scheduled:  Give Lecture 4 on Applied Programming             :BB:
Friday     6 April 2018
  2018-04-06: Scheduled:  Release of new Eels record
Saturday   7 April 2018
Sunday     8 April 2018
  2018-04-08: Scheduled:  TODO Celebrate Sunday

And lo and behold, this now works in org-journal as well! Just create a new journal entry in the future, either by pressing i j in M-x calendar or by calling org-journal-new-scheduled-entry, and org-journal will create an entry with a SCHEDULED property of the appropriate date (prefix to suppress TODO). When the current day reaches that entry, it will incorporate it into the daily journal.

Future journal entries are highlighted in M-x calendar, and you can get an overview of them with org-journal-schedule-view, or, if you enable org-journal-enable-agenda-integration, through the ordinary org-agenda, as shown above. The agenda integration does not include past journal entries in the agenda, since agenda searches tend to become very slow if they have to traverse the hundreds of files in my journal.

Footnotes:

1

: This is of course not his calendar, but mine.

Tags: org-journal org-mode
22 Mar 2018

Blogging with org-static-blog (Updated)

A while ago, I scratched an old itch and wrote my own static site generator, called org-static-blog. It is a simple thing: You hand it a directory full of *.org files with a #+title: and #+date:, and it assembles a bunch of HTML pages and an RSS feed from them. There are no external dependencies beyond Emacs.

Today, I released version 1.1.0 of org-static-blog, which introduces two new features: speed, and tags. You can now—optionally—add #+filetags: to your *.org files, (setq org-static-blog-enable-tags t), and org-static-blog will add tag links to every blog post, create tag indices, and add <category> tags to the RSS feed.

Update:

As Kaushal Modi pointed out in the comments, org-mode uses #+filetags: to set tags for entire files. My first implementation used #+tags:. This is now deprecated. #+tags: still works, but issues a warning, and will be removed in the future. Sorry for the inconvenience.

Tags: blog
17 Mar 2018

Speeding up org-static-blog

Three years ago, I had enough of all the static site generators out there. Over the life of this blog, I had used Octopress, then Pelican, then Coleslaw, then org-mode, and then wrote my own static site generator, org-static-blog. Above all, org-static-blog is simple. It iterates over all *.org files in org-static-blog-posts-directory, and then exports all of these files to HTML. Simple is good. Simple is reliable. Simple means I can fix things.

However, simple can also mean inefficient. Most glaringly, org-static-blog exports every single blog post three times every time you publish: Once to render the HTML, then once to render the RSS feed, then once to render the Index and Archive pages.

Today, I finally tackled this problem: Now, org-static-blog only exports each post once, when the *.org file changes. The RSS feed, the Index page, and the Archive page simply read the already-rendered HTML instead of exporting again.

Thus, a full rebuild of this blog and all of its 85 posts used to take 2:12 min, and now takes 42 s. More importantly, if only one org file changed, the rebuild used to take 1:08 min, and now takes 1.5 s. Things like this are hugely satisfying to me!

Tags: org-mode emacs blog
Other posts