Tidying up

This commit is contained in:
Edward Donner
2024-09-16 10:54:20 -04:00
parent 57cc8a44ef
commit 21afbd6c73
18 changed files with 512 additions and 19617 deletions

View File

@@ -12,7 +12,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"id": "e610bf56-a46e-4aff-8de1-ab49d62b1ad3",
"metadata": {},
"outputs": [],
@@ -33,7 +33,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"id": "4f672e1c-87e9-4865-b760-370fa605e614",
"metadata": {},
"outputs": [],
@@ -47,7 +47,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"id": "8aa149ed-9298-4d69-8fe2-8f5de0f667da",
"metadata": {},
"outputs": [],
@@ -62,7 +62,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"id": "6896636f-923e-4a2c-9d6c-fac07828a201",
"metadata": {},
"outputs": [],
@@ -74,7 +74,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"id": "8e7b3546-57aa-4c29-bc5d-f211970d04eb",
"metadata": {},
"outputs": [],
@@ -89,7 +89,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"id": "c6190659-f54c-4951-bef4-4960f8e51cc4",
"metadata": {},
"outputs": [],
@@ -103,7 +103,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": null,
"id": "71e1ba8c-5b05-4726-a9f3-8d8c6257350b",
"metadata": {},
"outputs": [],
@@ -118,7 +118,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"id": "e7d2fea8-74c6-4421-8f1e-0e76d5b201b9",
"metadata": {},
"outputs": [],
@@ -135,7 +135,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": null,
"id": "7cd84ad8-d55c-4fe0-9eeb-1895c95c4a9d",
"metadata": {},
"outputs": [],
@@ -157,7 +157,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": null,
"id": "a1cbb778-fa57-43de-b04b-ed523f396c38",
"metadata": {},
"outputs": [],
@@ -185,67 +185,20 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": null,
"id": "7fe1cd4b-d2c5-4303-afed-2115a3fef200",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Result: 3.141592658589\n",
"Execution Time: 8.576410 seconds\n"
]
}
],
"outputs": [],
"source": [
"exec(pi)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": null,
"id": "105db6f9-343c-491d-8e44-3a5328b81719",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"```cpp\n",
"#include <iostream>\n",
"#include <iomanip>\n",
"#include <chrono>\n",
"\n",
"double calculate(int iterations, int param1, int param2) {\n",
" double result = 1.0;\n",
" for (int i = 1; i <= iterations; ++i) {\n",
" double j = i * param1 - param2;\n",
" result -= (1.0 / j);\n",
" j = i * param1 + param2;\n",
" result += (1.0 / j);\n",
" }\n",
" return result;\n",
"}\n",
"\n",
"int main() {\n",
" auto start_time = std::chrono::high_resolution_clock::now();\n",
" \n",
" double result = calculate(100000000, 4, 1) * 4;\n",
" \n",
" auto end_time = std::chrono::high_resolution_clock::now();\n",
" std::chrono::duration<double> elapsed = end_time - start_time;\n",
"\n",
" std::cout << std::fixed << std::setprecision(12)\n",
" << \"Result: \" << result << std::endl\n",
" << \"Execution Time: \" << elapsed.count() << \" seconds\" << std::endl;\n",
"\n",
" return 0;\n",
"}\n",
"```"
]
}
],
"outputs": [],
"source": [
"optimize_gpt(pi)"
]
@@ -262,19 +215,10 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": null,
"id": "4194e40c-04ab-4940-9d64-b4ad37c5bb40",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Result: 3.141592658589\n",
"Execution Time: 0.213113375000 seconds\n"
]
}
],
"outputs": [],
"source": [
"!clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
"!./optimized"
@@ -282,65 +226,20 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": null,
"id": "983a11fe-e24d-4c65-8269-9802c5ef3ae6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"#include <iostream>\n",
"#include <iomanip>\n",
"#include <chrono>\n",
"\n",
"double calculate(int64_t iterations, int64_t param1, int64_t param2) {\n",
" double result = 1.0;\n",
" #pragma omp parallel for reduction(-:result)\n",
" for (int64_t i = 1; i <= iterations; ++i) {\n",
" double j = i * param1 - param2;\n",
" result -= (1.0 / j);\n",
" j = i * param1 + param2;\n",
" result += (1.0 / j);\n",
" }\n",
" return result;\n",
"}\n",
"\n",
"int main() {\n",
" auto start_time = std::chrono::high_resolution_clock::now();\n",
" double result = calculate(100'000'000, 4, 1) * 4;\n",
" auto end_time = std::chrono::high_resolution_clock::now();\n",
"\n",
" auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);\n",
"\n",
" std::cout << std::fixed << std::setprecision(12);\n",
" std::cout << \"Result: \" << result << std::endl;\n",
" std::cout << \"Execution Time: \" << duration.count() / 1e6 << \" seconds\" << std::endl;\n",
"\n",
" return 0;\n",
"}"
]
}
],
"outputs": [],
"source": [
"optimize_claude(pi)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": null,
"id": "d5a766f9-3d23-4bb4-a1d4-88ec44b61ddf",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Result: 3.141592658589\n",
"Execution Time: 0.212172000000 seconds\n"
]
}
],
"outputs": [],
"source": [
"!clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
"!./optimized"
@@ -348,7 +247,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": null,
"id": "c3b497b3-f569-420e-b92e-fb0f49957ce0",
"metadata": {},
"outputs": [],
@@ -399,132 +298,30 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": null,
"id": "dab5e4bc-276c-4555-bd4c-12c699d5e899",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total Maximum Subarray Sum (20 runs): 10980\n",
"Execution Time: 27.020543 seconds\n"
]
}
],
"outputs": [],
"source": [
"exec(python_hard)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": null,
"id": "e8d24ed5-2c15-4f55-80e7-13a3952b3cb8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"```cpp\n",
"#include <iostream>\n",
"#include <vector>\n",
"#include <limits>\n",
"#include <chrono>\n",
"\n",
"class LCG {\n",
" unsigned int value;\n",
" const unsigned int a = 1664525;\n",
" const unsigned int c = 1013904223;\n",
" const unsigned int m = 4294967296; // 2^32\n",
"public:\n",
" LCG(unsigned int seed) : value(seed) {}\n",
"\n",
" unsigned int next() {\n",
" value = (a * value + c) % m;\n",
" return value;\n",
" }\n",
"};\n",
"\n",
"long long max_subarray_sum(int n, unsigned int seed, int min_val, int max_val) {\n",
" LCG lcg(seed);\n",
" std::vector<int> random_numbers(n);\n",
" int range = max_val - min_val + 1;\n",
"\n",
" for (int i = 0; i < n; ++i) {\n",
" random_numbers[i] = lcg.next() % range + min_val;\n",
" }\n",
"\n",
" long long max_sum = std::numeric_limits<long long>::min();\n",
" for (int i = 0; i < n; ++i) {\n",
" long long current_sum = 0;\n",
" for (int j = i; j < n; ++j) {\n",
" current_sum += random_numbers[j];\n",
" if (current_sum > max_sum) {\n",
" max_sum = current_sum;\n",
" }\n",
" }\n",
" }\n",
"\n",
" return max_sum;\n",
"}\n",
"\n",
"long long total_max_subarray_sum(int n, unsigned int initial_seed, int min_val, int max_val) {\n",
" long long total_sum = 0;\n",
" LCG lcg(initial_seed);\n",
"\n",
" for (int i = 0; i < 20; ++i) {\n",
" unsigned int seed = lcg.next();\n",
" total_sum += max_subarray_sum(n, seed, min_val, max_val);\n",
" }\n",
"\n",
" return total_sum;\n",
"}\n",
"\n",
"int main() {\n",
" int n = 10000;\n",
" unsigned int initial_seed = 42;\n",
" int min_val = -10;\n",
" int max_val = 10;\n",
"\n",
" auto start_time = std::chrono::high_resolution_clock::now();\n",
" long long result = total_max_subarray_sum(n, initial_seed, min_val, max_val);\n",
" auto end_time = std::chrono::high_resolution_clock::now();\n",
"\n",
" std::chrono::duration<double> elapsed = end_time - start_time;\n",
"\n",
" std::cout << \"Total Maximum Subarray Sum (20 runs): \" << result << std::endl;\n",
" std::cout << \"Execution Time: \" << elapsed.count() << \" seconds\" << std::endl;\n",
"\n",
" return 0;\n",
"}\n",
"```"
]
}
],
"outputs": [],
"source": [
"optimize_gpt(python_hard)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": null,
"id": "e0b3d073-88a2-40b2-831c-6f0c345c256f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1moptimized.cpp:11:28: \u001b[0m\u001b[0;1;35mwarning: \u001b[0m\u001b[1mimplicit conversion from 'long' to 'const unsigned int' changes value from 4294967296 to 0 [-Wconstant-conversion]\u001b[0m\n",
" const unsigned int m = 4294967296; // 2^32\n",
"\u001b[0;1;32m ~ ^~~~~~~~~~\n",
"\u001b[0m1 warning generated.\n",
"Total Maximum Subarray Sum (20 runs): 0\n",
"Execution Time: 0.689923 seconds\n"
]
}
],
"outputs": [],
"source": [
"!clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
"!./optimized"
@@ -532,105 +329,20 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": null,
"id": "e9305446-1d0c-4b51-866a-b8c1e299bf5c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"#include <iostream>\n",
"#include <vector>\n",
"#include <chrono>\n",
"#include <limits>\n",
"#include <cstdint>\n",
"#include <iomanip>\n",
"\n",
"class LCG {\n",
"private:\n",
" uint64_t value;\n",
" const uint64_t a = 1664525;\n",
" const uint64_t c = 1013904223;\n",
" const uint64_t m = 1ULL << 32;\n",
"\n",
"public:\n",
" LCG(uint64_t seed) : value(seed) {}\n",
"\n",
" uint64_t next() {\n",
" value = (a * value + c) % m;\n",
" return value;\n",
" }\n",
"};\n",
"\n",
"int64_t max_subarray_sum(int n, uint64_t seed, int min_val, int max_val) {\n",
" LCG lcg(seed);\n",
" std::vector<int64_t> random_numbers(n);\n",
" for (int i = 0; i < n; ++i) {\n",
" random_numbers[i] = static_cast<int64_t>(lcg.next() % (max_val - min_val + 1) + min_val);\n",
" }\n",
"\n",
" int64_t max_sum = std::numeric_limits<int64_t>::min();\n",
" int64_t current_sum = 0;\n",
" \n",
" for (int i = 0; i < n; ++i) {\n",
" current_sum = std::max(current_sum + random_numbers[i], random_numbers[i]);\n",
" max_sum = std::max(max_sum, current_sum);\n",
" }\n",
" \n",
" return max_sum;\n",
"}\n",
"\n",
"int64_t total_max_subarray_sum(int n, uint64_t initial_seed, int min_val, int max_val) {\n",
" int64_t total_sum = 0;\n",
" LCG lcg(initial_seed);\n",
" for (int i = 0; i < 20; ++i) {\n",
" uint64_t seed = lcg.next();\n",
" total_sum += max_subarray_sum(n, seed, min_val, max_val);\n",
" }\n",
" return total_sum;\n",
"}\n",
"\n",
"int main() {\n",
" int n = 10000;\n",
" uint64_t initial_seed = 42;\n",
" int min_val = -10;\n",
" int max_val = 10;\n",
"\n",
" auto start_time = std::chrono::high_resolution_clock::now();\n",
" int64_t result = total_max_subarray_sum(n, initial_seed, min_val, max_val);\n",
" auto end_time = std::chrono::high_resolution_clock::now();\n",
"\n",
" auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);\n",
"\n",
" std::cout << \"Total Maximum Subarray Sum (20 runs): \" << result << std::endl;\n",
" std::cout << std::fixed << std::setprecision(6);\n",
" std::cout << \"Execution Time: \" << duration.count() / 1e6 << \" seconds\" << std::endl;\n",
"\n",
" return 0;\n",
"}"
]
}
],
"outputs": [],
"source": [
"optimize_claude(python_hard)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": null,
"id": "0c181036-8193-4fdd-aef3-fc513b218d43",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total Maximum Subarray Sum (20 runs): 10980\n",
"Execution Time: 0.001933 seconds\n"
]
}
],
"outputs": [],
"source": [
"!clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
"!./optimized"
@@ -638,7 +350,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": null,
"id": "0be9f47d-5213-4700-b0e2-d444c7c738c0",
"metadata": {},
"outputs": [],
@@ -654,7 +366,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": null,
"id": "8669f56b-8314-4582-a167-78842caea131",
"metadata": {},
"outputs": [],
@@ -675,7 +387,7 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": null,
"id": "2f1ae8f5-16c8-40a0-aa18-63b617df078d",
"metadata": {},
"outputs": [],
@@ -693,31 +405,10 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": null,
"id": "f1ddb38e-6b0a-4c37-baa4-ace0b7de887a",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div><iframe src=\"http://127.0.0.1:7862/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": []
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"with gr.Blocks() as ui:\n",
" with gr.Row():\n",
@@ -734,7 +425,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": null,
"id": "19bf2bff-a822-4009-a539-f003b1651383",
"metadata": {},
"outputs": [],
@@ -751,7 +442,7 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": null,
"id": "77f3ab5d-fcfb-4d3f-8728-9cacbf833ea6",
"metadata": {},
"outputs": [],
@@ -770,7 +461,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": null,
"id": "9a2274f1-d03b-42c0-8dcc-4ce159b18442",
"metadata": {},
"outputs": [],
@@ -783,31 +474,10 @@
},
{
"cell_type": "code",
"execution_count": 34,
"execution_count": null,
"id": "f1303932-160c-424b-97a8-d28c816721b2",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div><iframe src=\"http://127.0.0.1:7864/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": []
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"with gr.Blocks(css=css) as ui:\n",
" gr.Markdown(\"## Convert code from Python to C++\")\n",
@@ -857,7 +527,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.11.10"
}
},
"nbformat": 4,