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 / pgist.sh
Last active March 12, 2019 17:40 — forked from rubo77/create-gist.sh
Post GIST
#!/bin/bash
GITHUB_USERNAME=noureddin # change this to your github username
# or see https://gist.github.com/s-leroux/7cb7424d33ba3753e907cc2553bcd1ba
if [ "$1" == "" ]; then
printf "Usage: %s [--public] filename [gistname]\n" $(basename "$0") >&2
exit 1 # exit with error
fi
@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]
@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.

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 / 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 / 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 / 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