点击上方“C语言与CPP编程”,选择“关注/置顶/星标公众号”
干货福利,第一时间送达!
最近有小伙伴说没有收到当天的文章推送,这是因为微信改了推送机制,有一部分小伙伴刷不到当天的文章,一些比较实用的知识和信息,错过了就是错过了,建议大家加个星标⭐️,就能第一时间收到推送。
小伙伴们大家好,我是飞宇。
前段时间润去美国的师兄开了一门C/C++的课程,友情帮忙宣传一下,感兴趣的可以看看。
def estimate_pi( n_points: int, show_estimate: bool, ) -> None: """ Simple Monte Carlo Pi estimation calculation. Parameters ---------- n_points number of random numbers used to for estimation. show_estimate if True, will show the estimation of Pi, otherwise will not output anything. """ within_circle = 0 for _ in range(n_points): x, y = (random.uniform(-1, 1) for v in range(2)) radius_squared = x**2 + y**2 if radius_squared <= 1: within_circle += 1 pi_estimate = 4 * within_circle / n_points if not show_estimate: print("Final Estimation of Pi=", pi_estimate) def run_test( n_points: int, n_repeats: int, only_time: bool, ) -> None: """ Perform the tests and measure required time. Parameters ---------- n_points number of random numbers used to for estimation. n_repeats number of times the test is repeated. only_time if True will only print the time, otherwise will also show the Pi estimate and a neat formatted time. """ start_time = time.time() for _ in range(n_repeats): estimate_pi(n_points, only_time) if only_time: print(f"{(time.time() - start_time)/n_repeats:.4f}") else: print( f"Estimating pi took {(time.time() - start_time)/n_repeats:.4f} seconds per run." )
docker run -it --rm \
-v $PWD/your_script.py:/your_script.py \
python:3.11-rc-slim \
python /yourscript.py
def test_version(image: str) -> float:
"""
Run single_test on Python Docker image.
Parameter
---------
image
full name of the the docker hub Python image.
Returns
-------
run_time
runtime in seconds per test loop.
"""
output = subprocess.run([
'docker',
'run',
'-it',
'--rm',
'-v',
f'{cwd}/{SCRIPT}:/{SCRIPT}',
image,
'python',
f'/{SCRIPT}',
'--n_points',
str(N_POINTS),
'--n_repeats',
str(N_REPEATS),
'--only-time',
],
capture_output=True,
text=True,
)
avg_time = float(output.stdout.strip())
return avg_time
# Get test time for current Python version
base_time = test_version(NEW_IMAGE['image'])
print(f"The new {NEW_IMAGE['name']} took {base_time} seconds per run.\n")
# Compare to previous Python versions
for item in TEST_IMAGES:
ttime = test_version(item['image'])
print(
f"{item['name']} took {ttime} seconds per run."
f"({NEW_IMAGE['name']} is {(ttime / base_time) - 1:.1%} faster)"
)
The new Python 3.11 took 6.4605 seconds per run.
Python 3.5 took 11.3014 seconds.(Python 3.11 is 74.9% faster)
Python 3.6 took 11.4332 seconds.(Python 3.11 is 77.0% faster)
Python 3.7 took 10.7465 seconds.(Python 3.11 is 66.3% faster)
Python 3.8 took 10.6904 seconds.(Python 3.11 is 65.5% faster)
Python 3.9 took 10.9537 seconds.(Python 3.11 is 69.5% faster)
Python 3.10 took 8.8467 seconds.(Python 3.11 is 36.9% faster)
C 比 Python 快得多!
float estimate_pi(int n_points) {
double x, y, radius_squared, pi;
int within_circle=0;
for (int i=0; i < n_points; i++) {
x = (double)rand() / RAND_MAX;
y = (double)rand() / RAND_MAX;
radius_squared = x*x + y*y;
if (radius_squared <= 1) within_circle++;
}
pi=(double)within_circle/N_POINTS * 4;
return pi;
}
int main() {
double avg_time = 0;
srand(42);
for (int i=0; i < N_REPEATS; i++) {
auto begin = std::chrono::high_resolution_clock::now();
double pi = estimate_pi(N_POINTS);
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
avg_time += elapsed.count() * 1e-9;
printf("Pi is approximately %g and took %.5f seconds to calculate.\n", pi, elapsed.count() * 1e-9);
}
printf("\nEach loop took on average %.5f seconds to calculate.\n", avg_time / N_REPEATS);
}
g++ -o pi_estimate pi_estimate.c
Pi is approximately 3.14227 and took 0.25728 seconds to calculate.
Pi is approximately 3.14164 and took 0.25558 seconds to calculate.
Pi is approximately 3.1423 and took 0.25740 seconds to calculate.
Pi is approximately 3.14108 and took 0.25737 seconds to calculate.
Pi is approximately 3.14261 and took 0.25664 seconds to calculate.
Each loop took on average 0.25685 seconds to calculate.
—— EOF —— 你好,我是飞宇,本硕均于某中流985 CS就读,先后于百度搜索以及字节跳动电商等部门担任Linux C/C++后端研发工程师。
同时,我也是知乎博主@韩飞宇,日常分享C/C++、计算机学习经验、工作体会,欢迎点击此处查看我以前的学习笔记&经验&分享的资源。
我组建了一些社群一起交流,群里有大牛也有小白,如果你有意可以一起进群交流。
欢迎你添加我的微信,我拉你进技术交流群。此外,我也会经常在微信上分享一些计算机学习经验以及工作体验,还有一些内推机会。
加个微信,打开另一扇窗