Skip to content

Instantly share code, notes, and snippets.

@noureddin
noureddin / hyperpdfize.py
Created March 12, 2020 09:26
html2pdf converter using Blink/Chromium, using PyQt5 and PyQtWebEngine
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vim: set et sw=4 ts=4 :
# Pythonized from: https://doc.qt.io/qt-5/qtwebengine-webenginewidgets-html2pdf-example.html
# Differences from the C++ example:
# - handling local HTML files relative to the current working directory is added.
# - incognito mode is used.
# - removed tr() calls from the class.
# - a few cosemetic changes.
@noureddin
noureddin / svd.py
Last active August 11, 2018 01:07
using svd to “compress” an image, using opencv and numpy
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# do a lossy compression to an image using Single-value Decomposition (SVD)
# using numpy and opencv (and python)
import numpy as np
import cv2
from sys import argv
@noureddin
noureddin / magic.c
Last active July 16, 2018 18:57
using buffer overflow to loop
// prints the integers from 0 to 10
// compile with zero optimizations (use no `-O` flags, or only `-O0`)
// works on ubuntu xenial with gcc 5.4.0 (changed the buffer line from the original to work)
// src: https://old.reddit.com/r/ProgrammerHumor/comments/7p9c25/printing_all_integers_from_0_to_10/
#include <stdio.h>
int a[3][3][3][3][3] = {0};
void magic() {
@noureddin
noureddin / slideshare-dl.pl
Last active April 22, 2018 06:29
Download presentations from slideshare.net
#!/usr/bin/env perl
use strict; use warnings;
# download presentations from slideshare.net
sub help {
use File::Basename 'basename';
printf 'slideshare-dl - download presentations from slideshare.net
USAGE: %s -dl url_of_slides... [-r small|normal|full] [-q|-v]

Automated Verilog MIPS Testing

This documents our humble attempt at automating the testing of a MIPS project in Verilog using Perl.

We had two test scripts, general_test.pl and predefined_test.pl.

general_test.pl is made so that you can easily initialize some registers and pass a set of instructions and check the values of any number of registers after the execution; it was used for evaluating and grading the project.

@noureddin
noureddin / split.h
Last active February 3, 2018 21:47
modern replacement for strtok
#include <string>
#include <vector>
// If you are using strtok, or a variant like strtok_r or strtok_s, in your
// C++ code, you may be interested in this instead.
//
// This header contains a function called `split`, that takes a string, and
// an array of delimiting characters, and breaks the string on the delimiters,
// in a way similar to strtok, and returns a vector of strings representing
// the tokens.
@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
@noureddin
noureddin / mas.pl
Last active February 2, 2018 17:55
MIPS Assembler
#!/usr/bin/env perl
use strict; use warnings;
# a MIPS Assembler
# usage: ./mas.pl infile.asm > outfile.hex
# - it outputs to stdout; redirect it to a file if you need so.
# - exactly one file shall be supplied as arg.
# every line in the input file shall be either an instruction, a comment,
# an empty line (has spaces only), or a labeled instruction, which is a label
# followed by an instrucion on the same line. Lable-only lines are NOT well
# supported.
@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 / 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.