Skip to content

Instantly share code, notes, and snippets.

View UNIcodeX's full-sized avatar

Jared Fields UNIcodeX

View GitHub Profile
@UNIcodeX
UNIcodeX / LICENSE
Last active April 15, 2021 01:39 — forked from wlib/LICENSE
Run a shell script with bash, line-by-line, prompted on each command. Useful for running unknown scripts or debugging. Not a secure substitute for understanding a script beforehand.
MIT License
Copyright (c) 2021 Daniel Ethridge
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
# threadSafeFuture.nim
# Jens Alfke, 30 June 2020
import asyncdispatch, deques, locks, sugar, threadpool
## Utilities for mixing async/await with threads.
##
## ``threadSafe()`` takes a Future and returns a new Future that can be completed on any thread.
## The original Future's callback will still be invoked on its original thread, and ``await``
## works normally.
@UNIcodeX
UNIcodeX / workq.nim
Created November 12, 2020 14:49
A, shared heap, work queue written in Nim. From Treeform => https://gist.github.com/treeform/3e8c3be53b2999d709dadc2bc2b4e097
# nim c -r --threads:on --gc:orc
import cpuinfo, os, random, locks, deques
type
WorkReq = ref object
id: int
WorkRes = ref object
id: int
@UNIcodeX
UNIcodeX / floatingCSSButtons.html
Created October 16, 2020 13:12
Floating fixed position CSS Buttons
<html>
<style>
.floating-button {
z-index: 1000;
position: fixed;
border-radius: 33px;
padding: 20px;
text-decoration: none;
}
@UNIcodeX
UNIcodeX / stacklessGlobalMutate.py
Created August 13, 2020 16:52
Stackless threadlet global mutation
import stackless
class T:
metric = 0
instance_T = T
def increment_T_metric(instance):
instance.metric += 1
@UNIcodeX
UNIcodeX / xmonader-sequential_vs_async_vs_parallel-example.nim
Created April 17, 2020 15:03
This is an example from xmonader comparing the efficiency of a task when ran sequentially (synchronously), asynchronously, and in parallel.
# This came from https://github.com/xmonader/nim-linkcheck
# I just added the parallel example to the code and slightly modified the output formatting.
# My tests yielded the following numbers
# Sequential : 6.77 seconds
# Async : 0.27 seconds (25.07x faster | 3.988% time of sequential | 96.01 % faster)
# Parallel : 0.34 seconds (19.91x faster | 5.022% time of sequential | 94.98 % faster)
import os, httpclient
import strutils
import times
@UNIcodeX
UNIcodeX / parallelProcessingNoPointerThreadanalysisOn.nim
Created April 15, 2020 04:13
Example of parallel processing changing the value of a global variable without using a pointer. Instead it uses a `var` parameter, which automatically creates a pointer, as I understand it from Dom's book, Nim In Action.
import locks, threadpool, strutils
# compile with:
# nim c -r --threads:on -d:danger --gc:arc
{.experimental: "parallel".}
var
lock: Lock
a: seq[string]
initLock lock
@UNIcodeX
UNIcodeX / parallelProcessingGlobalVarWithExplicitPointerThreadanalysisOn.nim
Last active April 13, 2020 16:48
This example of the parallel manipulation of a global var works without `--threadanalysis:off`
import locks, threadpool, strutils
# compile with:
# nim c -r --threads:on -d:danger --gc:arc
{.experimental: "parallel".}
var
lock: Lock
a: array[20, string]
initLock lock
@UNIcodeX
UNIcodeX / parallelProcessingGlobalVarWithImplicitPointer.nim
Last active April 13, 2020 16:26
Example of using an implicitly defined pointer with parallel processing in Nim.
import locks, threadpool, strutils
# compile with:
# nim c -r --threads:on -d:danger --threadanalysis:off --gc:arc
{.experimental: "parallel".}
var
lock: Lock
a: array[20, string]
initLock lock
@UNIcodeX
UNIcodeX / parallelProcessingGlobalVarNoPointers.nim
Created April 13, 2020 16:13
Example of parallel processing NOT using pointers. NOTE: Sometimes one or more values does not get properly set in the array.
import locks, threadpool, strutils
{.experimental: "parallel".}
var
lock: Lock
a {.guard: lock.}: array[20, string]
initLock lock
proc process(s: string): string =