Skip to content

Instantly share code, notes, and snippets.

@dhruvbird
dhruvbird / data.txt
Last active August 29, 2015 14:15
All the code to compare regular binary search with 2-level binary search
# Columns:
# log(N) N Time/Iter(usec)[std::lower_bound] N Time/Iter(usec)[l2search]
23 8388608 0.460176 8388608 0.269376
24 16777216 0.621821 16777216 0.435599
25 33554432 0.735565 33554432 0.615605
26 67108864 0.856156 67108864 0.598811
27 134217728 0.959821 134217728 0.636344
28 268435456 1.552199 268435456 0.771024
29 536870912 1.158228 536870912 0.818436
30 1073741824 1.936374 1073741824 0.905859
@dhruvbird
dhruvbird / InPlaceMergeNsqrtN.cpp
Last active August 29, 2015 14:17
In-Place merge for 2 sorted arrays of size N and sqrt(N) each.
/* Compile and run:
*
* g++ -std=c++0x InPlaceMergeNsqrtN.cpp && ./a.out 23
*/
/* Merges 2 sorted arrays of size N and sqrt(N) in Theta(N) time
*
*/
#include <iostream>
#include <vector>
@dhruvbird
dhruvbird / range_union.py
Last active August 29, 2015 14:19
Range (Interval) Intersection
def unionRanges(intervals):
intervals = sorted(intervals) # Sort by elem[0]
ranges = [ ]
if len(intervals) == 0:
return [ ]
start = intervals[0][0]
end = intervals[0][1]
for i in range(1, len(intervals)):
if intervals[i][0] < end:
end = max(end, intervals[i][1])
#include <iostream>
#include <stack>
using namespace std;
// Counts numbers from 0 to n-1.
struct Counter {
int i = 0;
int n;
bool first = true; // Tracks if this is the first invocation of the next() function
Counter(int _n): n(_n) {}
#include <iostream>
using namespace std;
// Counts numbers from 0 to n-1.
struct Counter {
int i = 0;
int n;
bool first = true; // Tracks if this is the first invocation of the next() function
Counter(int _n): n(_n) {}
int* next() {
#include <iostream>
#include <stack>
using namespace std;
struct TreeNode {
int val;
TreeNode *left, *right;
TreeNode(int v): val(v), left(nullptr), right(nullptr) {}
};
@dhruvbird
dhruvbird / AddTensorsModel.py
Last active November 12, 2021 19:25
Simple Python code to create a very simple model
import torch
class AddTensorsModel(torch.nn.Module):
def __init__(self):
super().__init__();
self.t1 = torch.Tensor([0, 5, 10])
def helper(self, x, y):
z = self.t1
z = z + x + y
@dhruvbird
dhruvbird / RunModel.py
Created November 12, 2021 19:26
Run the model
m = AddtensorsModel()
res = m(torch.Tensor([1, 2, 3]), torch.Tensor([3, 4, 5]))
print(res)
@dhruvbird
dhruvbird / ScriptPyTorchModel.py
Created November 12, 2021 19:26
Create a scripted model
scripted = torch.jit.script(m)
@dhruvbird
dhruvbird / OptimizeModelForMobileInference.py
Created November 12, 2021 19:27
Optimize scripted model for mobile inference
from torch.utils.mobile_optimizer import optimize_for_mobile
optimized_model = optimize_for_mobile(scripted)