Skip to content

Instantly share code, notes, and snippets.

View rvprasad's full-sized avatar

Venkatesh-Prasad Ranganath rvprasad

View GitHub Profile
@rvprasad
rvprasad / AndroidManifest.xml
Last active September 17, 2023 04:20
Initialization Order of Android Components
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.dummy">
<application
android:label="@string/app_name"
tools:targetApi="31"
android:name=".MyApplication">
<provider
@rvprasad
rvprasad / synchronized_collection_issue.jsh
Last active December 18, 2021 22:17
Illustrates the absence of thread safety between various methods of SychronizedSet and its iteraror method.
Collection<Integer> ss = Collections.synchronizedSet(new HashSet<Integer>());
new Thread() {
public void run() {
for (int x = 0; x < 1000; x++) {
ss.addAll(IntStream.range(x*1000, (x + 1)*1000)
.boxed()
.collect(Collectors.toList()));
}
}
}.start()
@rvprasad
rvprasad / update_packages_to_new_r_version.r
Last active October 2, 2021 21:49
Update existing packages to new version of R
library(gtools)
installed <- c()
update <- function(p) {
pd <- packageDescription(p, fields=c('Built'))
pd <- gsub(";.*", "", pd)
if (!is.na(pd) && pd != 'R 4.0.4') { # mention the target versions here
for (d in getDependencies(p)) {
update(d)
@rvprasad
rvprasad / hostfile
Last active October 31, 2022 11:07
Compare map's performance of mpi4py module and Python's multiprocessing module
localhost slots=9
@rvprasad
rvprasad / python_performance1.py
Last active December 24, 2019 22:51
Compare performance of set and list in identifying unique values
import random
import statistics
from timeit import default_timer as timer
def time_function(f, data):
times = []
for _ in range(0, 20):
start = timer()
f(data)
times.append(timer() - start)
@rvprasad
rvprasad / modification_time_based_file_organizer.py
Last active December 23, 2019 05:31
Group files based on the their modification time. Great for organizing photos and videos. Requires Python 3.7+.
import argparse
import glob
import hashlib
import logging
import os
import pathlib
import re
import shutil
import sys
import time
@rvprasad
rvprasad / test_mulitprocessing_shared_memory.py
Last active July 8, 2022 15:43
Explores performance of shared memory support available in multiprocessing library in Python 3.8.
# Python -- v3.8
from multiprocessing import Process, Queue, managers
import time
def worker(id, data, queue, *args):
tmp1 = time.time()
if args:
for i in range(args[0], args[1]):
@rvprasad
rvprasad / variance.kt
Last active April 21, 2019 04:11
Understanding Contravariance and Covariance via Kotlin
class Producer<out T: Any>(val e:T) {
fun read(): T = e
}
class Consumer<in T: Any>() {
private lateinit var e: T
fun write(v: T): Unit { e = v }
}
fun main() {
@rvprasad
rvprasad / howManyLongsCanWeLoad.scala
Last active March 7, 2019 21:14
A script to identify how many longs can we store at a time with given heap size on JVM
import scala.collection.mutable.ArrayBuffer
import scala.util.Random
object M {
def main(args: Array[String]) {
val t = new ArrayBuffer[Long]()
while (true) {
t += Random.nextLong
if (t.length % 10000 == 0) {
System.gc()
@rvprasad
rvprasad / parallelStreamAndAsSequence.kts
Last active February 21, 2019 05:02
Java parallel stream is significantly faster than the same stream converted into a Kotlin sequence
import kotlin.streams.asSequence
import kotlin.system.measureTimeMillis
val k = 500000000L
println(measureTimeMillis {
java.util.stream.LongStream.range(1, k)
.parallel()
.asSequence()
.map { it.toString().length.toLong() }