Skip to content

Instantly share code, notes, and snippets.

@ydm
Created June 24, 2024 21:45
Show Gist options
  • Save ydm/2115feb25b8bdff2ff0c0fa315571198 to your computer and use it in GitHub Desktop.
Save ydm/2115feb25b8bdff2ff0c0fa315571198 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "4328f390-5149-4405-9f7c-8a9f0f56eaf9",
"metadata": {},
"outputs": [],
"source": [
"from math import ceil\n",
"\n",
"\n",
"def level(n, w, d):\n",
" '''\n",
" n: number of validators\n",
" w: number of workers\n",
" d: duration per task\n",
" '''\n",
" return ceil(n/w) * d\n",
"\n",
"\n",
"def predict(n, w):\n",
" '''\n",
" n: number of validators\n",
" w: number of workers\n",
" '''\n",
" ans = level(n, w, 30)\n",
" for i in range(1, 31+1):\n",
" v = ceil(n / 2**i)\n",
" x = level(v, w, 1.5)\n",
" ans += x\n",
" return ans\n",
"\n",
"\n",
"def estimate(n, t=3000):\n",
" '''\n",
" n: number of validators\n",
" t: time limit in seconds\n",
" '''\n",
" low = 1\n",
" high = 2**10\n",
" while low <= high:\n",
" w = (low + high) // 2\n",
" x = predict(n, w)\n",
" if x < t:\n",
" high = w-1\n",
" elif x > t:\n",
" low = w+1\n",
" else:\n",
" return w\n",
" return low"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "d7bd977d-b05f-4a9b-8591-97f5692b1f71",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"estimate(100)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "970205d0-23b7-4375-95f6-4e5e2e713661",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"11"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"estimate(1000)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "13d492f7-69b7-49b9-8976-4d6bec292bf0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"107"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"estimate(10000)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment