Skip to content

Instantly share code, notes, and snippets.

\documentclass{report}
\usepackage{caption}
\usepackage{polyglossia}
\setmainlanguage{arabic}
\newfontfamily\arabicfont[Script=Arabic,Scale=1]{KacstOne}
% https://tex.stackexchange.com/questions/350777/side-by-side-figures-are-reversed-in-lof-bidi-xetex
\newcommand{\dblimg}
[7][ht]{{%
\stepcounter{figure}
@noureddin
noureddin / egist.sh
Created January 29, 2017 06:26
Edit GIST
#!/bin/bash
# edit a gist file contents: https://developer.github.com/v3/gists/#edit-a-gist
# based on https://gist.github.com/noureddin/93d6182acdd1dcdf880daee1b97af05f
GITHUB_USERNAME=noureddin # change this to your github username
if [ $# -lt 2 ]; then
printf "Usage: %s id filename [gistname]\n" $(basename "$0") >&2
exit 1 # exit with error
fi
@noureddin
noureddin / fullcircle-downloader.sh
Created January 29, 2017 19:33
FullCircle magazine downloader
#!/bin/bash
if [ -z "$1" ]; then
wget -qc --show-progress http://dl.fullcirclemagazine.org/issue{117..0}_en.pdf 2>/dev/null
else
wget -qc --show-progress http://dl.fullcirclemagazine.org/issue${1}_en.pdf
fi
@noureddin
noureddin / dvqw.py
Created February 27, 2017 16:28
Dvorak-QWERTY Converter
#!/usr/bin/python3
# translate dvorak to qwetry and vice versa
# thank you, tr; you're useless to me!
# usage:
# run it, and input a line of dvorak-encoded text, then press enter to get the qwerty-decoded one
# run it with any argument, and input a line of qwerty, then press enter to get dvorak
h = {}
h[']'] = '/'; h['}'] = '+'
@noureddin
noureddin / frequency-counter.sh
Last active March 12, 2017 03:46
Letters and letter combinations frequency counter in POSIX shell
#!/bin/sh
## Introduction
# This script calculates the frequencies of letters and letter combinations (monograms, bigrams, trigrams) in a given text. The frequency is given as the percentage of occurrences.
# This script is language- and script-agnostic; it should work with any script whatsoever (it's tested with Arabic). It's written in POSIX sh, and uses Unix tools, so it should work on most OSes. Please note it's case-sensitive.
# This script is licensed under the terms of CC0. Attribution is really appreciated but not required.
# Contact the author at noureddin@protonmail.com or noureddin95@gmail.com
# defining a function that changes the numbered output of `uniq -c | sort -nr` to percentage
numbered2percent()
@noureddin
noureddin / get_linux_voice.sh
Created March 22, 2017 18:13
Download Linux Voice magazines and optimize them for screens
#!/bin/bash
# Download Linux Voice magazines and optimize them for screens
# which makes them less than half in size
get_issue() {
temp_file=Linux-Voice-Issue-0$1_.pdf
final_file=Linux-Voice-Issue-0$1.pdf
[ -e $final_file ] && return
wget -qc --show-progress -O $temp_file \
https://www.linuxvoice.com/issues/0$1/Linux-Voice-Issue-0$1.pdf &&
ps2pdf -dSETTING=/ebook $temp_file $final_file &&
@noureddin
noureddin / youtube-shelf-dl
Created July 29, 2017 18:29
Download a shelf of playlists from Youtube
#!/usr/bin/env perl
# Download a shelf of playlists from Youtube
# using wget, perl and youtube-dl.
# After using it, you'll find folders with the playlists'
# names, and each has a file named '.get'. Issue `. .get`
# in your terminal to start downloading the playlist.
# You can run `for i in *; do cd "$i"; . .get; cd ..; done`
# to get them all after this script.
# License: CC0
@noureddin
noureddin / performance-1d-vs-2d-arrays.md
Last active November 9, 2017 22:28
Dynamically allocated arrays: 1D vs 2D

Dynamically allocated arrays: 1D vs 2D

Summary: In C++, implementing a 2D array as a 1D array can provide about 125% to 130% performance gain. It's not much if your application is not performance critical, but the handling code of 1D arrays is still far simpler than that of 2D arrays.

Overview

Let us write a small C++ class for matrices, with multiplying and printing functions. And let us benchmark it to see how can we make it most performant. Obviously we need to choose a performant algorithm; e.g., using Strassen algorithm is far better than multiplying matrices the naïve way. But I will do it the naïve way just to focus on one aspect only, namely, the performance impact of using 2D vs 1D dynamically allocated arrays.

I will compile using GCC (g++) with the highest level of optimization (-O3). I am using GCC 5.4.0.

@noureddin
noureddin / performance-separate-interface-implementation.md
Last active November 15, 2017 01:30
Performance of C (and C++) functions: separate implementation and interface, or implementation in header?

Performance of C (and C++) functions: separate implementation and interface, or implementation in header?

Summary: In C, implementing a function in a header file, rather than a separate .c file, provides about 200% performance gain. Maybe it's because the compiler is inlining them. But you may not gain that much in nontrivial projects.
In C++, you need also to apply link-time optimization, with the compiler switch -flto, to have that 200% performance gain.

Note 1: In many projects, especially large ones, the bottleneck is not those non-inlined functions, and separating the implementation from the interface makes compilation faster, thus easier to develop.

Note 2: Algorithmic optimization is most important (chose better algorithms). If you really need more performance, investigate other possibilities, like parallelizing your code, vectorize some parts of it, etc.

Overview

@noureddin
noureddin / tic-tac-toe.pl
Last active January 6, 2018 21:52
a simple, two-player tic-tac-toe in perl
#!/usr/share/env perl
# a simple, two-player tic-tac-toe game
use strict; use warnings;
my @cells;
my %valid_input = map { $_ => undef } (1..9);
my $who_has_turn = 1; # 1 or 2, for player 1 or 2, respectively