Skip to content

Instantly share code, notes, and snippets.

View th0j's full-sized avatar
💭
🚀 🚀

Tho Vo th0j

💭
🚀 🚀
View GitHub Profile
@th0j
th0j / sodoku.c
Last active October 4, 2018 15:41
#include <stdio.h>
#include <stdlib.h>
int main() {
int n = 3;
int m = 3;
int a[3][3] = { 0 };
a[1][1] = 5;
int x = 5;
@th0j
th0j / measure.rb
Created September 16, 2018 15:41
measure Ruby performance
def measure(no_gc = true, &block)
if no_gc
GC.disable
else
GC.start
end
memory_before = `ps -o rss= -p #{Process.pid}`.to_i/1024
gc_stat_before = GC.stat
time = Benchmark.realtime do
yield
@th0j
th0j / import_large_sql.sh
Created September 14, 2018 01:19
Import a large sql dump file to a MySQL database from command line
#!/bin/sh
# store start date to a variable
imeron=`date`
echo "Import started: OK"
dumpfile="/home/bob/bobiras.sql"
ddl="set names utf8; "
ddl="$ddl set global net_buffer_length=1000000;"
@th0j
th0j / benchmark.rb
Created August 21, 2018 04:48
Benchmark ruby
sizes = [10, 50, 100, 1000]
sizes.each do |size|
array = size.times.map { |i| rand(size) }
threshold = size / 2
Benchmark.bmbm(2) do |x|
x.report("select #{size}") do
array.select { |item| item > threshold }
.each { |item| item * item }
@th0j
th0j / mysql_note.md
Created July 30, 2018 04:38
mysql note

Change password

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '';
@th0j
th0j / split_sql.rb
Created July 30, 2018 03:59
Split big sql file
filename = 'flowdata_201803052230.sql'
INSERT_MATCHER = 'INSERT INTO'
file = 1
counter = 1
File.open(filename, 'r').each do |line|
next unless line.include? INSERT_MATCHER
File.open(file.to_s + '.sql', 'a+') { |file| file.write("\n" + line) }
@th0j
th0j / Install_Emacs.md
Last active November 30, 2019 00:12
Install emacs 26.1 on Ubuntu 18.03

Emacs dependencies

sudo apt-get install build-essential texinfo libx11-dev libxpm-dev libjpeg-dev libpng-dev libgif-dev libtiff-dev libgtk2.0-dev libncurses-dev libxpm-dev automake autoconf

Build emacs

mkdir emacs
cd emacs
@th0j
th0j / access_control.rb
Created July 23, 2018 03:23
Access control in ruby
class A
protected
def protected_a
puts "Protected A"
end
private
def private_a
puts "Private A"
end
@th0j
th0j / CONDING_RULE.md
Last active July 20, 2018 06:39
Conding convention

1. Hạn chế việc sử dụng if else lồng nhau. Ở đầu function nên return các case sai sau đó mới viết logic cho function

2. Khi viết SQL không được nối chuỗi mà phải sử dụng param truyền vào câu truy vấn.

3. Sử dụng Active record hoặc Arel over raw queries

Example:

    # unless ids.blank?
    #   sql = "DELETE FROM links WHERE links.user_id = #{user_id.to_s} AND links.id IN (#{ids.to_s})"
    #   connection.execute(sql)
    # end
 return if ids.blank?