1679395469
libcmaes is a multithreaded C++11 implementation (with Python bindings) of algorithms of the CMA-ES family for optimization of nonlinear non-convex 'blackbox' functions. The implemented algorithms have a wide range of applications in various disciplines, ranging from pure function minimization, optimization in industrial and scientific applications, to the solving of reinforcement and machine learning problems.
Over the past decade, both the original CMA-ES and its improved flavors have proven very effective in optimizing functions when no gradient is available. Typically, the algorithm does find the minimum value of an objective function in a minimal number of function calls, compared to other methods. For a full report of recent results, see (3).
CMA-ES is mostly the work of Nikolaus Hansen (4) and a few others (8). Other implementations can be found in (5).
Main functionalities: At the moment, the library implements many of the most effective CMA-ES algorihms, and defaults to the 'vanilla' version (1). Current features include:
Documentation:
Dependencies:
Implementation: The library makes use of C++ policy design for modularity, performance and putting the maximum burden on the checks at compile time. The implementation closely follows the algorithms described in (2), (6) and few other publications.
Below are instruction for Linux systems, for building on Mac OSX, see https://github.com/beniz/libcmaes/wiki/Building-libcmaes-on-Mac-OSX
Beware of dependencies, typically on Debian/Ubuntu Linux, do:
sudo apt-get install autoconf automake libtool libgoogle-glog-dev libgflags-dev libeigen3-dev
For compiling with basic options enabled:
./autogen.sh
echo "#define CMAES_EXPORT" > include/libcmaes/cmaes_export.h
./configure
make
For compiling with CMake:
mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=~/.local/
make -j2
make install
cd tests
./test_functions --dim 30 --lambda 100 --max_iter 120 --fname fsphere
to minimize the sphere function in 30D with 100 offsprings per generation,
./test_functions --dim 20 --lambda 100 --max_iter 1000 --fname rosenbrock
to minimize the Rosenbrock function in 20D with 100 offsprings. To see available function, do
./test_functions --list
to plot results, use a file output and then the included Gnuplot script
./test_functions --fname rastrigin --dim 10 --lambda 200 --max_iter 130 --fplot out.dat -sigma0 5 -x0 5 -seed 5489
gnuplot -e "filename='out.dat'" cma_multiplt.dem
to plot results with matplotlib instead
python ../python/cma_multiplt.py out.dat
to run a check across a range of classical single-objective optimization functions:
./test_functions --all
for help, do
./test_functions --help
#include "cmaes.h"
#include <iostream>
using namespace libcmaes;
FitFunc fsphere = [](const double *x, const int N)
{
double val = 0.0;
for (int i=0;i<N;i++)
val += x[i]*x[i];
return val;
};
int main(int argc, char *argv[])
{
int dim = 10; // problem dimensions.
std::vector<double> x0(dim,10.0);
double sigma = 0.1;
//int lambda = 100; // offsprings at each generation.
CMAParameters<> cmaparams(x0,sigma);
//cmaparams.set_algo(BIPOP_CMAES);
CMASolutions cmasols = cmaes<>(fsphere,cmaparams);
std::cout << "best solution: " << cmasols << std::endl;
std::cout << "optimization took " << cmasols.elapsed_time() / 1000.0 << " seconds\n";
return cmasols.run_status();
}
To build the Python bindings and use libcmaes from Python code, see instructions below, and for more details, see https://github.com/beniz/libcmaes/wiki/Python-bindings
sudo apt-get install libboost-python-dev
./autogen.sh
./configure --enable-python --prefix=/home/yourusername
make
make install
and with cmake
:
cmake .. -DCMAKE_INSTALL_PREFIX=~/.local/ -DLIBCMAES_BUILD_PYTHON=ON
cd python
export LD_LIBRARY_PATH=/home/yourusername/lib
python ptest.py
Sample python code:
import lcmaes
# input parameters for a 10-D problem
x = [10]*10
olambda = 10 # lambda is a reserved keyword in python, using olambda instead.
seed = 0 # 0 for seed auto-generated within the lib.
sigma = 0.1
p = lcmaes.make_simple_parameters(x,sigma,olambda,seed)
# objective function.
def nfitfunc(x,n):
val = 0.0
for i in range(0,n):
val += x[i]*x[i]
return val
# generate a function object
objfunc = lcmaes.fitfunc_pbf.from_callable(nfitfunc);
# pass the function and parameter to cmaes, run optimization and collect solution object.
cmasols = lcmaes.pcmaes(objfunc,p)
# collect and inspect results
bcand = cmasols.best_candidate()
bx = lcmaes.get_candidate_x(bcand)
print "best x=",bx
print "distribution mean=",lcmaes.get_solution_xmean(cmasols)
cov = lcmaes.get_solution_cov(cmasols) # numpy array
print "cov=",cov
print "elapsed time=",cmasols.elapsed_time(),"ms"
CMA-ES requires two components from the user:
In short: the optimum that is looked after should better not be far away from the interval [x0 - sigma0, x0 + sigma0] in each dimension, where distance is defined by sigma0.
See https://github.com/beniz/libcmaes/wiki/Practical-hints and https://www.lri.fr/~hansen/cmaes_inmatlab.html#practical for more detailed useful advices using CMA-ES.
There's an install script in the repository. Do:
cd tests
./bbobsetup.sh
you can now benchmark any of the implemented flavors of CMA-ES (beware, this make take a while, ~hours):
./bbobexperiment -alg bipop
for the command above, results will be in repository bipop_bbob See (7) for more information and details.
This work was supported by the ANR-2010-COSI-002 grant of the French NationalA Research Agency.
Author: CMA-ES
Source Code: https://github.com/CMA-ES/libcmaes
License: Unknown, Unknown licenses found
1679198460
The Computational Geometry Algorithms Library (CGAL) is a C++ library that aims to provide easy access to efficient and reliable algorithms in computational geometry.
CGAL Releases
The primary vector of distribution of CGAL are source tarballs, released twice a year, announced on the web site of CGAL.
Getting Started with CGAL
Since version 5.0, CGAL is a header-only library, meaning that it is no longer needed to build CGAL libraries before it can be used.
Head over to the CGAL manual for usage guides and tutorials that will get you started smoothly.
CGAL Git Repository Layout
The Git repository of CGAL has a different layout from release tarballs. It contains a CMakeLists.txt
file that serves as anchor for configuring and building programs, and a set of subfolders, so called packages. Most packages implement a data structure or an algorithm for CGAL (e.g., Convex_hull_2
, or Triangulation_3
); however some packages serve special needs:
Installation
- meta-files and CMake-supportMaintenance
- infrastructural supportCore
, CGALimageIO
, Qt_widget
, GraphicsView
- component librariesScripts
- scripts to simplify developer's and user's workTestsuite
- infrastructure for testsuiteDocumentation
- infrastructure for CGAL's manualSTL_Extension
- extensions to the standard template libraryMore Information
Author: CGAL
Source Code: https://github.com/CGAL/cgal
License: View license
1679113320
Tìm hiểu cách triển khai Thuật toán tìm kiếm nhị phân (Binary Search) trong C++. Trong hướng dẫn này, bạn sẽ thấy cách triển khai thuật toán tìm kiếm nhị phân trong C++. Thuật toán tìm kiếm nhị phân trong C++ (Giải thích bằng ví dụ)
Thuật toán tìm kiếm nhị phân là một thuật toán phân chia và chinh phục mà bạn có thể sử dụng để tìm kiếm và tìm các phần tử trong một mảng được sắp xếp.
Thuật toán này nhanh chóng tìm kiếm các phần tử vì nó loại bỏ một nửa mảng mỗi khi lặp lại tìm kiếm.
Vì vậy, thay vì tìm kiếm trong toàn bộ mảng, thuật toán sẽ loại bỏ một nửa mảng nơi không thể tìm thấy phần tử được tìm kiếm. Nó làm điều này liên tục cho đến khi phần tử được tìm thấy.
Trong trường hợp phần tử được tìm kiếm không tồn tại, nó sẽ trả về giá trị -1. Nếu phần tử tồn tại, thì nó trả về chỉ mục của phần tử.
Nếu các giải thích ở trên có vẻ phức tạp, thì bạn nên xem hướng dẫn trực quan này về cách thức hoạt động của thuật toán tìm kiếm nhị phân .
Trong bài viết này, bạn sẽ thấy cách triển khai thuật toán tìm kiếm nhị phân trong C++.
Bắt đầu nào!
Trong phần này, chúng tôi sẽ chia nhỏ và giải thích việc triển khai tìm kiếm nhị phân trong C++.
Đây là mã:
#include <iostream>
using namespace std;
int binarySearch(int array[], int low, int high, int number_to_search_for) {
while (low <= high) {
int mid = low + (high - low) / 2;
if (number_to_search_for == array[mid]){
return mid;
}
if (number_to_search_for > array[mid]){
low = mid + 1;
}
if (number_to_search_for < array[mid]){
high = mid - 1;
}
}
return -1;
}
int main(void) {
int arrayOfNums[] = {2,4,7,9,10,13,20};
int n = sizeof(arrayOfNums) / sizeof(arrayOfNums[0]);
int result = binarySearch(arrayOfNums, 0, n - 1, 13);
if (result == -1){
printf("Element doesn't exist in the array");
}
else{
printf("The index of the element is %d", result);
}
// The index of the element is 5
}
Để bắt đầu, chúng tôi đã tạo một phương thức được gọi binarySearchcó bốn tham số:
Tiếp theo, chúng tôi đã tạo một whilevòng lặp sẽ chạy liên tục cho đến khi thao tác tìm kiếm trả về một giá trị – chỉ mục của phần tử hoặc -1.
Trong whilevòng lặp:
midđược sử dụng để đại diện cho trung tâm/trung điểm của mảng: int mid = low + (high - low) / 2;.
Câu lệnh đầu tiên ifđược thực hiện nếu midcó cùng giá trị với phần tử được tìm kiếm:
if (number_to_search_for == array[mid]){
return mid;
}
Câu lệnh thứ hai ifdi chuyển vị trí của lowchỉ mục sau điểm giữa của mảng:
if (number_to_search_for > array[mid]){
low = mid + 1;
}
Điều này loại bỏ tất cả các phần tử ở phía bên trái của mảng vì phần tử được tìm kiếm lớn hơn chúng, vì vậy không cần phải tìm kiếm trong phần đó của mảng.
Câu lệnh thứ ba iflàm ngược lại với câu lệnh thứ hai – nó di chuyển vị trí cao đến chỉ mục trước điểm giữa của mảng:
if (number_to_search_for < array[mid]){
high = mid - 1;
}
Đây là một bản tóm tắt của mã trong ifbáo cáo:
Nếu số cần tìm không tồn tại, -1 sẽ được trả về. Bạn có thể thấy điều này sau whilevòng lặp trong mã.
Cuối cùng, chúng tôi đã thử nghiệm binarySearchphương pháp:
int main(void) {
int arrayOfNums[] = {2,4,7,9,10,13,20};
int n = sizeof(arrayOfNums) / sizeof(arrayOfNums[0]);
int result = binarySearch(arrayOfNums, 0, n - 1, 13);
if (result == -1){
printf("Element doesn't exist in the array");
}
else{
printf("The index of the element is %d", result);
}
// The index of the element is 5
}
Trong đoạn mã trên, chúng tôi đã chuyển vào các giá trị của các tham số được tạo trong phương binarySearchthức: binarySearch(arrayOfNums, 0, n -1, 13).
Khi bạn chạy mã, "Chỉ mục của phần tử là 5" sẽ được in trong bảng điều khiển. Điều này là do chỉ số của số cần tìm (13) là 5.
Bạn có thể nghịch mã bằng cách thay đổi giá trị của số cần tìm.
Trong bài viết này, chúng ta đã nói về việc triển khai thuật toán tìm kiếm nhị phân trong C++.
Chúng ta đã thấy một ví dụ mã có binarySearchphương thức nhận tham số, tìm kiếm thông qua một mảng số và trả về giá trị thích hợp sau thao tác tìm kiếm.
Chúng tôi cũng đã thấy một lời giải thích chi tiết về từng phần của mã.
Chúc mừng mã hóa!
Nguồn: https://www.freecodecamp.org
#cplusplus #cpp #datastructures #algorithms
1679108248
Learn how to implement Binary Search Algorithm in C++. In this tutorial, you will see an implementation of the binary search algorithm in C++. Binary Search Algorithm in C++ (Explained with Examples)
The binary search algorithm is a divide and conquer algorithm that you can use to search for and find elements in a sorted array.
The algorithm is fast in searching for elements because it removes half of the array every time the search iteration happens.
So instead of searching through the whole array, the algorithm removes half of the array where the element to be searched for can't be found. It does this continuously until the element is found.
In a case where the element to be searched for doesn't exist, it returns a value of -1. If the element exists, then it returns the index of the element.
If the explanations above seem complex, then you should check out this visual guide on how the binary search algorithm works.
In this article, you'll see an implementation of the binary search algorithm in C++.
Let's get started!
In this section, we'll break down and explain the implementation of binary search in C++.
Here's the code:
#include <iostream>
using namespace std;
int binarySearch(int array[], int low, int high, int number_to_search_for) {
while (low <= high) {
int mid = low + (high - low) / 2;
if (number_to_search_for == array[mid]){
return mid;
}
if (number_to_search_for > array[mid]){
low = mid + 1;
}
if (number_to_search_for < array[mid]){
high = mid - 1;
}
}
return -1;
}
int main(void) {
int arrayOfNums[] = {2,4,7,9,10,13,20};
int n = sizeof(arrayOfNums) / sizeof(arrayOfNums[0]);
int result = binarySearch(arrayOfNums, 0, n - 1, 13);
if (result == -1){
printf("Element doesn't exist in the array");
}
else{
printf("The index of the element is %d", result);
}
// The index of the element is 5
}
To start with, we created a method called binarySearch
which had four parameters:
array[]
represents the array to be searched through.low
represents the first element of the array.high
represents the last element of the array.number_to_search_for
represents the number to be searched for in array[]
.Next, we created a while
loop that will run continuously until the search operation returns a value – either the index of the element or -1.
In the while
loop:
mid
is used to represent the center/midpoint of the array: int mid = low + (high - low) / 2;
.
The first if
statement is executed if mid
has the same value as the element to be searched for:
if (number_to_search_for == array[mid]){
return mid;
}
The second if
statement moves the position of low
to the index after the midpoint of the array:
if (number_to_search_for > array[mid]){
low = mid + 1;
}
This removes all the elements on the left side of the array because the element to be searched for is greater than they are, so there is no need to search through that part of the array.
The third if
statement does the opposite of the second statement – it moves the position of high to the index before the midpoint of the array:
if (number_to_search_for < array[mid]){
high = mid - 1;
}
Here's a summary of the code in the if
statements:
mid
, the mid
index will be returned.mid
, search through the elements on the right side of mid
.mid
, search through the elements on the left side of mid
.If the number to be searched for doesn't exist, -1 will be returned. You can see this after the while
loop in the code.
Lastly, we tested the binarySearch
method:
int main(void) {
int arrayOfNums[] = {2,4,7,9,10,13,20};
int n = sizeof(arrayOfNums) / sizeof(arrayOfNums[0]);
int result = binarySearch(arrayOfNums, 0, n - 1, 13);
if (result == -1){
printf("Element doesn't exist in the array");
}
else{
printf("The index of the element is %d", result);
}
// The index of the element is 5
}
In the code above, we passed in the values of the parameters created in the binarySearch
method: binarySearch(arrayOfNums, 0, n -1, 13)
.
arrayOfNums
represents the array to be searched for: {2,4,7,9,10,13,20}.0
represents the first index of the array.n - 1
represents the last index of the array. Have a look at the code to see how n
was created.13
is the number to be searched for in arrayOfNums
.When you run the code, "The index of the element is 5" will be printed in the console. This is because the index of the number to be searched for (13) is 5.
You can play around with the code by changing the value of the number to be searched for.
In this article, we talked about the implementation of the binary search algorithm in C++.
We saw a code example that had a binarySearch
method which took in parameters, searched through an array of numbers, and returned the appropriate value after the search operation.
We also saw a detailed explanation of each part of the code.
Happy coding!
Source: https://www.freecodecamp.org
#cplusplus #cpp #datastructures #algorithms
1679021160
toppra
: Time-Optimal Path ParameterizationOverview
toppra is a library for computing the time-optimal path parametrization for robots subject to kinematic and dynamic constraints. In general, given the inputs:
p(s)
, s
in [0, s_end]
;toppra returns the time-optimal path parameterization: s_dot (s)
, from which the fastest trajectory q(t)
that satisfies the given constraints can be found.
Documentation and tutorials are available here.
You can install the package with pip:
pip install toppra
To install from source for development:
pip install -r requirement3.txt
pip install -e .
Support
Please report any issues, questions or feature request via Github issues tracker.
Have a quick question? Try asking in our slack channel.
Pull Requests are welcome! Create a Pull Request and we will review your proposal!
Credits
toppra
was originally developed by Hung Pham (Eureka Robotics, former CRI Group) and Phạm Quang Cưong (Eureka Robotics, CRI Group) with major contributions from talented contributors:
If you have taken part in developing and supporting the library, feel free to add your name to the list.
The development is also generously supported by Eureka Robotics.
Citing toppra
If you use this library for your research, we encourage you to
Author: Hungpham2511
Source Code: https://github.com/hungpham2511/toppra
License: MIT license
#machinelearning #python #robotics #motion #planning #algorithms
1678413420
NLopt is a library for nonlinear local and global optimization, for functions with and without gradient information. It is designed as a simple, unified interface and packaging of several free/open-source nonlinear optimization libraries.
The latest release can be downloaded from the NLopt releases page on Github, and the NLopt manual is hosted on readthedocs.
NLopt is compiled and installed with the CMake build system (see CMakeLists.txt
file for available options):
git clone https://github.com/stevengj/nlopt.git
cd nlopt
mkdir build
cd build
cmake ..
make
sudo make install
(To build the latest development sources from git, you will need SWIG to generate the Python and Guile bindings.)
Once it is installed, #include <nlopt.h>
in your C/C++ programs and link it with -lnlopt -lm
. You may need to use a C++ compiler to link in order to include the C++ libraries (which are used internally by NLopt, even though it exports a C API). See the C reference manual.
There are also interfaces for C++, Fortran, Python, Matlab or GNU Octave, OCaml, GNU Guile, GNU R, Lua, Rust, and Julia. Interfaces for other languages may be added in the future.
Author: stevengj
Source Code: https://github.com/stevengj/nlopt
License: View license
1678332670
In this tutorial, we'll learn the basics of what a binary search tree is, what the various parts of a binary tree are, and the common terms associated with a tree: Inorder, Preorder, Post Order for BST
We will also see how to traverse a tree using some of the common algorithms – all illustrated with clear examples.
A binary search tree is a binary tree made up of nodes. Each node has a key signifying its value.
The value of the nodes on the left subtree are smaller than the value of the root node. And the value of the nodes on the right subtree are larger than the value of the root node.
The root node is the parent node of both subtrees.
The diagram below shows the main parts of a binary tree:
Diagram of a binary search tree
Let's us look at the relationship between the nodes.
A
is the root node.B
while the right subtree begins at C
.A
has two child nodes – B
and C
.C
is the parent node to F
and G
. F
and G
are siblings.F
and G
are know as leaf nodes because they do not have children.B
is the parent node to D
and E
.D
is the parent node to H
and I
.D
and E
are siblings as well as H
and I
.E
is a leaf node.So here are some important terms that we just used to describe the tree above:
Root: The topmost node in the tree.
Parent: A node with a child or children.
Child: A node extended from another node (parent node).
Leaf: A node without a child.
Binary search trees help us speed up our binary search as we are able to find items faster.
We can use the binary search tree for the addition and deletion of items in a tree.
We can also represent data in a ranked order using a binary tree. And in some cases, it can be used as a chart to represent a collection of information.
Next, we'll look at some techniques used in traversing a binary tree.
Traversing a tree means visiting and outputting the value of each node in a particular order. In this tutorial, we will use the Inorder, Preorder, and Post order tree traversal methods.
The major importance of tree traversal is that there are multiple ways of carrying out traversal operations unlike linear data structures like arrays, bitmaps, matrices where traversal is done in a linear order.
Each of these methods of traversing a tree have a particular order they follow:
Here is another way of representing the information above:
Inorder => Left, Root, Right.
Preorder => Root, Left, Right.
Post order => Left, Right, Root.
We are going to create a tree similar to the one in the last section, but this time the node keys will be numbers instead of letters.
Remember that the values of the nodes on the left subtree are always smaller than the value of the root node. Also, the values of the nodes on the right subtree are larger than the value of the root node.
Here is the diagram we will be working with:
Recall that the order for inorder traversal is Left, Root, Right.
This is the result we get after using inorder traversal:
D, B, E, A, F, C, G
If that seems a bit complex for you to understand, then follow the order of the colors in the picture below
Inorder traversal
The order here is Root, Left, Right.
Using the same diagram above, we have:
A, B, D, E, C, F, G
Here is the same diagram with different colors used as a guide:
Preorder traversal
The order for post order traversal is Left, Right, Root.
Here is the output:
D, E, B, F, G, C, A
If you can't figure out how we arrived at that result, then use the colors in the picture below as a guide:
Postorder traversal
In this tutorial, we learned the basics of what a binary search tree is, what the various parts of a binary tree are, and the common terms associated with a tree. We also saw some of the algorithms we can use to traverse a tree.
Thank you for reading!
Source: https://www.freecodecamp.org
#datastructures #algorithms
1677928140
DEAP is a novel evolutionary computation framework for rapid prototyping and testing of ideas. It seeks to make algorithms explicit and data structures transparent. It works in perfect harmony with parallelisation mechanisms such as multiprocessing and SCOOP.
DEAP includes the following features:
Following acceptance of PEP 438 by the Python community, we have moved DEAP's source releases on PyPI.
You can find the most recent releases at: https://pypi.python.org/pypi/deap/.
See the DEAP User's Guide for DEAP documentation.
In order to get the tip documentation, change directory to the doc
subfolder and type in make html
, the documentation will be under _build/html
. You will need Sphinx to build the documentation.
Also checkout our new notebook examples. Using Jupyter notebooks you'll be able to navigate and execute each block of code individually and tell what every line is doing. Either, look at the notebooks online using the notebook viewer links at the botom of the page or download the notebooks, navigate to the you download directory and run
jupyter notebook
We encourage you to use easy_install or pip to install DEAP on your system. Other installation procedure like apt-get, yum, etc. usually provide an outdated version.
pip install deap
The latest version can be installed with
pip install git+https://github.com/DEAP/deap@master
If you wish to build from sources, download or clone the repository and type
python setup.py install
DEAP build status is available on Travis-CI https://travis-ci.org/DEAP/deap.
The most basic features of DEAP requires Python2.6. In order to combine the toolbox and the multiprocessing module Python2.7 is needed for its support to pickle partial functions. CMA-ES requires Numpy, and we recommend matplotlib for visualization of results as it is fully compatible with DEAP's API.
Since version 0.8, DEAP is compatible out of the box with Python 3. The installation procedure automatically translates the source to Python 3 with 2to3, however this requires having setuptools<=58
. It is recommended to use pip install setuptools==57.5.0
to address this issue.
The following code gives a quick overview how simple it is to implement the Onemax problem optimization with genetic algorithm using DEAP. More examples are provided here.
import random
from deap import creator, base, tools, algorithms
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)
toolbox = base.Toolbox()
toolbox.register("attr_bool", random.randint, 0, 1)
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, n=100)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
def evalOneMax(individual):
return sum(individual),
toolbox.register("evaluate", evalOneMax)
toolbox.register("mate", tools.cxTwoPoint)
toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)
toolbox.register("select", tools.selTournament, tournsize=3)
population = toolbox.population(n=300)
NGEN=40
for gen in range(NGEN):
offspring = algorithms.varAnd(population, toolbox, cxpb=0.5, mutpb=0.1)
fits = toolbox.map(toolbox.evaluate, offspring)
for fit, ind in zip(fits, offspring):
ind.fitness.values = fit
population = toolbox.select(offspring, k=len(population))
top10 = tools.selBest(population, k=10)
Authors of scientific papers including results generated using DEAP are encouraged to cite the following paper.
@article{DEAP_JMLR2012,
author = " F\'elix-Antoine Fortin and Fran\c{c}ois-Michel {De Rainville} and Marc-Andr\'e Gardner and Marc Parizeau and Christian Gagn\'e ",
title = { {DEAP}: Evolutionary Algorithms Made Easy },
pages = { 2171--2175 },
volume = { 13 },
month = { jul },
year = { 2012 },
journal = { Journal of Machine Learning Research }
}
If you want your project listed here, send us a link and a brief description and we'll be glad to add it.
Author: DEAP
Source Code: https://github.com/DEAP/deap
License: LGPL-3.0 license
1677900008
Gym is an open source Python library for developing and comparing reinforcement learning algorithms by providing a standard API to communicate between learning algorithms and environments, as well as a standard set of environments compliant with that API. Since its release, Gym's API has become the field standard for doing this.
To install the base Gym library, use pip install gym
.
This does not include dependencies for all families of environments (there's a massive number, and some can be problematic to install on certain systems). You can install these dependencies for one family like pip install gym[atari]
or use pip install gym[all]
to install all dependencies.
We support Python 3.7, 3.8, 3.9 and 3.10 on Linux and macOS. We will accept PRs related to Windows, but do not officially support it.
The Gym API's API models environments as simple Python env
classes. Creating environment instances and interacting with them is very simple- here's an example using the "CartPole-v1" environment:
import gym
env = gym.make("CartPole-v1")
observation, info = env.reset(seed=42)
for _ in range(1000):
action = env.action_space.sample()
observation, reward, terminated, truncated, info = env.step(action)
if terminated or truncated:
observation, info = env.reset()
env.close()
Please note that this is an incomplete list, and just includes libraries that the maintainers most commonly point newcommers to when asked for recommendations.
Gym keeps strict versioning for reproducibility reasons. All environments end in a suffix like "_v0". When changes are made to environments that might impact learning results, the number is increased by one to prevent potential confusion.
The latest "_v4" and future versions of the MuJoCo environments will no longer depend on mujoco-py
. Instead mujoco
will be the required dependency for future gym MuJoCo environment versions. Old gym MuJoCo environment versions that depend on mujoco-py
will still be kept but unmaintained. To install the dependencies for the latest gym MuJoCo environments use pip install gym[mujoco]
. Dependencies for old MuJoCo environments can still be installed by pip install gym[mujoco_py]
.
A whitepaper from when Gym just came out is available https://arxiv.org/pdf/1606.01540, and can be cited with the following bibtex entry:
@misc{1606.01540,
Author = {Greg Brockman and Vicki Cheung and Ludwig Pettersson and Jonas Schneider and John Schulman and Jie Tang and Wojciech Zaremba},
Title = {OpenAI Gym},
Year = {2016},
Eprint = {arXiv:1606.01540},
}
There used to be release notes for all the new Gym versions here. New release notes are being moved to releases page on GitHub, like most other libraries do. Old notes can be viewed here.
Important Notice
Gym documentation website is at https://www.gymlibrary.dev/, and you can propose fixes and changes to it here.
Gym also has a discord server for development purposes that you can join here: https://discord.gg/nHg2JRN489
Author: openai
Source Code: https://github.com/openai/gym
License: View license
1677808154
An Averaged Shifted Histogram (ASH) is essentially Kernel Density Estimation over a fine-partition histogram. ASH uses constant memory, can be constructed on-line via O(nbins) updates, and lets you estimate densities for arbitrarily big data.
import Pkg
Pkg.add("AverageShiftedHistograms")
using AverageShiftedHistograms
ash(randn(10^6))
Author: joshday
Source Code: https://github.com/joshday/AverageShiftedHistograms.jl
License: View license
1677765069
Analisi della complessità temporale di Python vs C++. Le differenze dei tempi di esecuzione tra Python e C++. In questo tutorial vedrai 3 algoritmi e come si comportano in C++ e Python. Quanto è più veloce C++ rispetto a Python?
La velocità è importante nei linguaggi di programmazione e alcuni vengono eseguiti molto più velocemente di altri.
Ad esempio, potresti sapere che C++ è più veloce di Python. Allora perché è così?
Bene, C++ è un linguaggio che utilizza un compilatore, per non parlare del fatto che è un linguaggio di programmazione di livello molto inferiore rispetto a Python. Vale a dire, il C++ fornisce molta meno astrazione dal set di istruzioni e dall'architettura di un computer.
D'altra parte, Python è un linguaggio interpretato, il che significa semplicemente che ogni riga del programma viene valutata mentre il programma è in esecuzione.
Ma quanto è più veloce IS C++ rispetto a Python? In questo articolo vedrai 3 algoritmi e come si comportano in C++ e Python. Questi algoritmi provengono da "Data Structures and Algorithms Using C++" di Michael T. Goodrich.
Nota: il computer su cui ho eseguito questi test era un Acer Swift 3. La CPU era una Ryzen 7 7500U con Radeon Graphics 1.80Gz. Insieme a 8 GB di RAM, con Windows 10 Home.
Vedrai questi algoritmi e la loro Big O Notation . Big O è un modo matematico di esprimere lo scenario peggiore della complessità temporale o spaziale di un algoritmo.
La complessità temporale è la quantità di tempo necessaria per l'esecuzione di un algoritmo, mentre la complessità spaziale è la quantità di spazio (memoria) occupata da un algoritmo. Ecco un grafico per aiutare a spiegare Big O:
Big O Cheat Sheet – Grafico della complessità temporale (freecodecamp.org)
D'ora in poi, mi riferirò a ciascun algoritmo come eseguito con una certa complessità temporale.
Ad esempio, se dico che un algoritmo viene eseguito con una complessità temporale O(n) , ciò significa che man mano che l'input cresce, il tempo necessario per l'esecuzione di un algoritmo è linear . Se dico che funziona con una complessità temporale O(n^2) , ciò significa che man mano che l'input cresce, il tempo necessario per l'esecuzione di un algoritmo è quadratic .
Algorithm Ex1(A):
Input: An array "A" storing n >= 1 integers.
Output: The sum of the elements in A.
s = A[0]
for i = 1 to n - 1 do:
s = s + A[i]
return s
Pseudo-codice per il 1° algoritmo
In Python, possiamo esprimere questo algoritmo come:
def ex1(A):
sum = A[0]
for i in A:
sum = sum + A[i]
return sum
Codice Python per il primo algoritmo in Python
Se utilizzi input fino a circa 5 milioni, vedrai che questo algoritmo impiega circa 4 secondi per funzionare con un input così grande.
Prestazioni del 1° algoritmo
Quindi puoi confrontare questo algoritmo in C++, in questo modo:
double ex1(double A[], size_t n)
{
double sum = A[0];
for(size_t i = 0; i < n; i++)
{
s = s + A[i];
}
return sum;
}
Codice C++ per il primo algoritmo
Ti starai chiedendo, cos'è size_t? size_tè un "intero senza segno". Questo significa solo che questa variabile non ha un segno. Il che significa anche solo che questa variabile non è negativa .
In C++, usiamo size_tper tenere traccia delle posizioni in un array, poiché tali posizioni non possono essere negative (almeno in C++, poiché in Python possiamo avere indici negativi).
Ora diamo un'occhiata al tempo di esecuzione del primo algoritmo:
Prestazioni del 1° algoritmo in C++
As you can see from these two charts, the time complexity seems to be linear. This means that the time complexity of this algorithm is O(n) – as the input size for A grows, the time it takes for this algorithm to run grows linearly.
But it is interesting to notice that for very large inputs of 5 million, C++ does not even break the 1 second mark. Whereas Python breaks the 3-4 second mark for inputs at about 5 million. Let's move on to our next algorithm.
Algorithm Ex3(A):
Input: An array "A" storing n >= 1 integers.
Output: The sum of the prefix sums in A.
s = 0
for i = 0 to n - 1 do:
s = s + A[0]
for j = 1 to i do:
s = s + A[j]
return s
Pseudo-Code for the 2nd Algorithm
Here, you can see that this algorithm has two for-loops. So, moving forward this is something to acknowledge when analyzing the time complexity.
In Python, you can express this algorithm like this:
def ex3(A):
sum = 0
for i in range(len(A)):
sum = sum + A[i]
for j in range(1, i):
sum = sum + A[j]
return sum
Python Code for 2nd Algorithm
Usiamo anche gli input fino a 70.000. Quindi registreremo i tempi e tracciamo i dati in questo modo:
Prestazioni temporali del 2° algoritmo in Python
Questi risultati sono molto diversi dai nostri precedenti grafici dell'algoritmo 1. Qualcosa da sottolineare è che con un input di 70.000, questo algoritmo impiega ben 91 secondi in Python. È tanto tempo!
Inoltre, quando ho provato a eseguire input superiori a 70.000, il mio computer ha iniziato a non rispondere. Accidenti.
Diamo un'occhiata a questo algoritmo in C++:
double ex3(double A[], size_t n)
{
double s = 0;
for(size_t i = 0; i < n; i++)
{
s = s + A[i];
for(size_t j = 1; j < i; j++)
{
s = s + A[j];
}
}
return s;
}
Codice C++ per il secondo algoritmo
Per il codice C++ sono stato in grado di aumentare la dimensione dell'input a ~90.000.
Prestazioni temporali del 2° algoritmo in C++
Per una dimensione di input di 70.000, questo algoritmo impiega circa 4 secondi per essere eseguito in C++. Questa differenza è enorme. Per non parlare del fatto che sono stato in grado di utilizzare input di dimensioni di circa ~ 90.000 elementi per C ++, senza nemmeno superare i 10 secondi.
Inoltre, possiamo vedere che la curva non è liscia come il grafico di Python. Ciò potrebbe essere dovuto ad altri processi in esecuzione in background (poiché sono in esecuzione su Windows 10) o per qualche altro motivo.
Inoltre, possiamo classificare la complessità temporale di questo algoritmo come O(n^2) , il che significa semplicemente che la complessità temporale per questo algoritmo è quadratica.
Passiamo all'ultimo algoritmo.
Algorithm Ex5(A, B):
Input: Arrays "A" and "B" each storing n >= 1 integers.
Output: The number of elements in B equal to the sum of
prefix sums in A.
c = 0
for i = 0 to n - 1 do:
s = 0
for j = 0 to n - 1 do:
s = s + A[0]
for k = 1 to j do:
s = s + A[k]
if B[i] == s then:
c = c + 1
return c
Pseudo-codice per il 3° algoritmo
Diamo un'occhiata a questo codice in Python:
def ex3(A, B):
c = 0
for i in range(len(A)):
s = 0
for j in range(len(A)):
s = s + A[0]
for k in range(1, j):
s = s + A[k]
if B[i] == s:
c = c + 1
return c
Codice Python per il terzo algoritmo
Analizziamo ora le tempistiche:
Time Performance of the 3rd Algorithm in Python
Woah, what's going on here? We can see that as our inputs starts to increase, the rate at which the algorithm runs also starts to increase, but it is increasing at a drastic rate.
In this case, we can see with an input size of 3500 that it takes 761 seconds for this algorithm to run in Python. You may be asking, "Did you actually sit through all 761 seconds?", the answer is yes. Yes, I did.
Now let's take a look at the C++ code:
double ex5(double A[], double B[], size_t n)
{
double c = 0;
for(size_t i = 0; i < n; i++)
{
double s = 0;
for(size_t j = 0; j < n; i++)
{
s = s + A[0]
for(size_t k = 1; k < j; k++)
{
s = s + A[k];
}
if(B[i] == s)
{
c = c + 1
}
}
}
return c;
}
C++ Code for the 3rd Algorithm
Let's take a look at the graph.
Time Performance of the 3rd Algorithm in C++
Similar to the second algorithm, it is interesting to see that the curve is not as smooth as the Python graph.
Inoltre, possiamo vedere che possiamo andare ben al di sopra di una dimensione di input di 3500. Il mio computer ha iniziato a funzionare male quando ho spinto la dimensione di input oltre 10.000 per C++. Per non parlare del fatto che con una dimensione di input di 10.000 l'algoritmo ha una media di circa 544-545 secondi.
Possiamo classificare questo algoritmo come avente una complessità temporale di O(n!). Il che significa solo che questo algoritmo è fattoriale, che funziona molto lentamente.
Spero che tu abbia trovato le differenze dei tempi di esecuzione tra Python e C++ affascinanti quanto me.
Inoltre, solo perché Python funziona più lentamente del C++ per ogni algoritmo non significa che il C++ sia il linguaggio "migliore". Entrambi questi linguaggi hanno i propri scopi per il tipo di software che stai cercando di creare.
C++ sarebbe il linguaggio preferito se le prestazioni sono fondamentali. Se stavi programmando giochi, sistemi operativi o comunicando tra macchinari, C++ sarebbe la scelta migliore grazie alla sua natura compilata e veloce.
Python sarebbe preferito se hai bisogno di sviluppare software rapidamente. Grazie alla sua curva di apprendimento più semplice, quasi chiunque può prendere Python e iniziare a creare software con esso. Python fornisce anche molte risorse per la scienza dei dati e l'apprendimento automatico.
Fonte: https://www.freecodecamp.org
#python #cplusplus #cpp #datastructures #algorithms
1677761460
Python vs C++ 時間複雜度分析。Python 和 C++ 運行時間的差異。在本教程中,您將看到 3 種算法以及它們在 C++ 和 Python 中的表現。與 Python 相比,C++ 快多少?
速度在編程語言中很重要,有些語言的執行速度比其他語言快得多。
例如,您可能知道 C++比 Python更快。那麼為什麼會這樣呢?
好吧,C++ 是一種使用編譯器的語言,更不用說它是一種比 Python 低得多的編程語言。也就是說,C++對計算機的指令集和體系結構提供的抽像要少得多。
另一方面,Python 是一種解釋型語言,這只是意味著程序的每一行都會在程序運行時進行評估。
但是,與 Python 相比,C++ 快多少呢?在本文中,您將看到 3 種算法以及它們在 C++ 和 Python 中的表現。這些算法來自 Michael T. Goodrich 的“Data Structures and Algorithms Using C++”。
注意:我運行這些測試的計算機是 Acer Swift 3。CPU 是帶有 Radeon Graphics 1.80Gz 的 Ryzen 7 7500U。連同 8GB 內存,運行 Windows 10 家庭版。
您將看到這些算法及其大 O 表示法。 Big O 是一種表達算法時間或空間複雜度的最壞情況的數學方法。
時間複雜度是算法運行所需的時間量,而空間複雜度是算法佔用的空間(內存)量。這是一張有助於解釋 Big O 的圖表:
Big O 備忘單——時間複雜度圖表 (freecodecamp.org)
從現在開始,我將把每個算法稱為以特定的時間複雜度運行。
例如,如果我說一個算法以O(n)時間複雜度運行,這意味著隨著輸入的增長,算法運行所需的時間是線性的。如果我說它以O(n^2)時間複雜度運行,這意味著隨著輸入的增長,算法運行所需的時間是二次方的。
Algorithm Ex1(A):
Input: An array "A" storing n >= 1 integers.
Output: The sum of the elements in A.
s = A[0]
for i = 1 to n - 1 do:
s = s + A[i]
return s
第一種算法的偽代碼
在 Python 中,我們可以將這個算法表示為:
def ex1(A):
sum = A[0]
for i in A:
sum = sum + A[i]
return sum
Python 中第一個算法的 Python 代碼
如果您使用高達大約 500 萬的輸入,您會發現該算法需要大約 4 秒才能運行這麼大的輸入。
第一種算法的性能
然後你可以在 C++ 中比較這個算法,像這樣:
double ex1(double A[], size_t n)
{
double sum = A[0];
for(size_t i = 0; i < n; i++)
{
s = s + A[i];
}
return sum;
}
第一種算法的 C++ 代碼
你可能想知道,什麼是size_t?size_t是一個“無符號整數”。這只是意味著這個變量沒有符號。這也只是意味著這個變量不是負數。
在 C++ 中,我們用來size_t跟踪數組中的位置,因為這些位置不能為負(至少在 C++ 中,因為在 Python 中我們可以有負索引)。
現在讓我們看一下第一個算法的運行時間:
第一個算法在 C++ 中的性能
從這兩個圖表可以看出,時間複雜度似乎是線性的。這意味著該算法的時間複雜度為O(n) – 隨著輸入大小的A增長,該算法運行所需的時間線性增長。
但有趣的是,對於 500 萬的非常大的輸入,C++ 甚至沒有突破 1 秒標記。而 Python 在大約 500 萬次輸入時打破了 3-4 秒的記錄。讓我們繼續我們的下一個算法。
Algorithm Ex3(A):
Input: An array "A" storing n >= 1 integers.
Output: The sum of the prefix sums in A.
s = 0
for i = 0 to n - 1 do:
s = s + A[0]
for j = 1 to i do:
s = s + A[j]
return s
第二種算法的偽代碼
在這裡,您可以看到該算法有兩個 for 循環。因此,向前推進這是在分析時間複雜性時要承認的事情。
在 Python 中,你可以這樣表達這個算法:
def ex3(A):
sum = 0
for i in range(len(A)):
sum = sum + A[i]
for j in range(1, i):
sum = sum + A[j]
return sum
第二種算法的 Python 代碼
讓我們也使用一直到 70,000 的輸入。然後我們將記錄時間並繪製數據圖表,如下所示:
Python 中第二種算法的時間性能
這些結果與我們之前算法 1 的圖表有很大不同。需要指出的是,在輸入 70,000 的情況下,該算法在 Python 中耗時高達 91 秒。那是很長一段時間了!
此外,當我嘗試運行高於 70,000 的輸入時,我的計算機開始變得無響應。哎呀。
讓我們用 C++ 看一下這個算法:
double ex3(double A[], size_t n)
{
double s = 0;
for(size_t i = 0; i < n; i++)
{
s = s + A[i];
for(size_t j = 1; j < i; j++)
{
s = s + A[j];
}
}
return s;
}
第二種算法的 C++ 代碼
對於 C++ 代碼,我能夠將輸入大小增加到 ~90,000。
第二種算法在 C++ 中的時間性能
對於 70,000 的輸入大小,此算法在 C++ 中運行大約需要 4 秒。這種差異是巨大的。更不用說,我能夠為 C++ 使用大小約為 90,000 個元素的輸入,甚至沒有超過 10 秒標記。
此外,我們可以看到曲線不像 Python 圖形那樣平滑。這可能是由於某些其他進程在後台運行(因為我在 Windows 10 上運行)或其他一些原因。
此外,我們可以將此算法的時間複雜度歸類為O(n^2),這僅意味著此算法的時間複雜度是二次的。
讓我們繼續最後一個算法。
Algorithm Ex5(A, B):
Input: Arrays "A" and "B" each storing n >= 1 integers.
Output: The number of elements in B equal to the sum of
prefix sums in A.
c = 0
for i = 0 to n - 1 do:
s = 0
for j = 0 to n - 1 do:
s = s + A[0]
for k = 1 to j do:
s = s + A[k]
if B[i] == s then:
c = c + 1
return c
第三種算法的偽代碼
讓我們用 Python 看一下這段代碼:
def ex3(A, B):
c = 0
for i in range(len(A)):
s = 0
for j in range(len(A)):
s = s + A[0]
for k in range(1, j):
s = s + A[k]
if B[i] == s:
c = c + 1
return c
第三種算法的 Python 代碼
現在,讓我們分析一下時間:
第三種算法在 Python 中的時間性能
哇,這是怎麼回事?我們可以看到,隨著我們的輸入開始增加,算法運行的速率也開始增加,但它正在以急劇的速度增加。
在這種情況下,我們可以看到輸入大小為 3500,該算法在 Python 中運行需要 761 秒。你可能會問,“你真的坐了 761 秒嗎?”,答案是肯定的。是的,我做到了。
現在讓我們看一下C++代碼:
double ex5(double A[], double B[], size_t n)
{
double c = 0;
for(size_t i = 0; i < n; i++)
{
double s = 0;
for(size_t j = 0; j < n; i++)
{
s = s + A[0]
for(size_t k = 1; k < j; k++)
{
s = s + A[k];
}
if(B[i] == s)
{
c = c + 1
}
}
}
return c;
}
第三種算法的 C++ 代碼
讓我們看一下圖表。
第三個算法在 C++ 中的時間性能
與第二種算法類似,有趣的是曲線並不像 Python 圖那樣平滑。
此外,我們可以看到我們可以遠遠超過 3500 的輸入大小。一旦我將 C++ 的輸入大小推到 10,000 以上,我的計算機就開始出現問題。更不用說,輸入大小為 10,000 的算法平均耗時約 544-545 秒。
我們可以將此算法分類為具有 O(n!) 的時間複雜度。這只是意味著該算法是階乘的,運行速度非常慢。
我希望您能像我一樣發現 Python 和 C++ 之間運行時間的差異。
此外,僅僅因為 Python 在每種算法上都比 C++ 運行得慢,並不意味著 C++ 是“更好”的語言。對於您嘗試創建的軟件類型,這兩種語言都有自己的用途。
如果性能至關重要,C++ 將是首選語言。如果您正在編寫遊戲、操作系統或機器之間的通信,C++ 將是更好的選擇,因為它具有編譯性和快速的特性。
如果您需要快速開發軟件,Python 將是首選。由於它更容易學習,幾乎任何人都可以學習 Python 並開始使用它創建軟件。Python 還為數據科學和機器學習提供了許多資源。
資料來源: https: //www.freecodecamp.org
#python #cplusplus #cpp #datastructures #algorithms
1677757800
การวิเคราะห์ความซับซ้อนของเวลา Python vs C ++ ความแตกต่างของเวลาทำงานระหว่าง Python และ C++ ในบทช่วยสอนนี้ คุณจะเห็นอัลกอริทึม 3 รายการและวิธีดำเนินการใน C++ และ Python C ++ เร็วกว่า Python เท่าไหร่
ความเร็วเป็นสิ่งสำคัญในภาษาการเขียนโปรแกรม และบางภาษาก็ทำงานได้เร็วกว่าภาษาอื่นมาก
ตัวอย่างเช่น คุณอาจรู้ว่า C++ เร็วกว่า Python เหตุใดจึงเป็นเช่นนี้
C++ เป็นภาษาที่ใช้คอมไพเลอร์ ไม่ต้องพูดถึงว่ามันเป็นภาษาโปรแกรมระดับต่ำกว่า Python มาก กล่าวคือ C++ ให้สิ่งที่เป็นนามธรรมจากชุดคำสั่งและสถาปัตยกรรมของคอมพิวเตอร์ น้อยกว่ามาก
ในทางกลับกัน Python เป็นภาษาที่ตีความได้ ซึ่งหมายความว่าทุกบรรทัดของโปรแกรมจะได้รับการประเมินในขณะที่โปรแกรมกำลังทำงาน
แต่ IS C ++ เร็วกว่าเมื่อเทียบกับ Python เท่าไหร่ ในบทความนี้ คุณจะเห็นอัลกอริทึม 3 รายการและวิธีดำเนินการใน C++ และ Python อัลกอริทึมเหล่านี้มาจาก "โครงสร้างข้อมูลและอัลกอริทึมโดยใช้ C++" โดย Michael T. Goodrich
หมายเหตุ: คอมพิวเตอร์ที่ฉันทำการทดสอบเหล่านี้คือ Acer Swift 3 CPU คือ Ryzen 7 7500U พร้อม Radeon Graphics 1.80Gz พร้อมกับ RAM 8GB รัน Windows 10 Home
คุณจะได้ดูอัลกอริทึมเหล่านี้และสัญลักษณ์Big O Big O เป็นวิธีทางคณิตศาสตร์ในการแสดงสถานการณ์ที่เลวร้ายที่สุดของเวลาหรือพื้นที่ที่ซับซ้อนของอัลกอริทึม
ความซับซ้อนของเวลาคือระยะเวลาที่อัลกอริทึมใช้ในการทำงาน ในขณะที่ความซับซ้อนของพื้นที่คือจำนวนของพื้นที่ (หน่วยความจำ) ที่อัลกอริทึมใช้ นี่คือกราฟที่ช่วยอธิบาย Big O:
Big O Cheat Sheet – แผนภูมิความซับซ้อนของเวลา (freecodecamp.org)
จากนี้ไป ฉันจะอ้างถึงแต่ละอัลกอริทึมว่าทำงานด้วยความซับซ้อนของเวลา
ตัวอย่างเช่น ถ้าฉันพูดว่าอัลกอริทึมทำงานด้วย ความซับซ้อนของเวลา O(n)หมายความว่าเมื่ออินพุตเพิ่มขึ้น เวลาที่อัลกอริทึมใช้ในการรันจะเป็นเชิงเส้น ถ้าฉันบอกว่ามันทำงานด้วย ความซับซ้อนของเวลา O(n^2)หมายความว่าเมื่ออินพุตโตขึ้น เวลาที่อัลกอริทึมใช้ในการรันจะเป็นกำลังสอง
Algorithm Ex1(A):
Input: An array "A" storing n >= 1 integers.
Output: The sum of the elements in A.
s = A[0]
for i = 1 to n - 1 do:
s = s + A[i]
return s
Pseudo-Code สำหรับอัลกอริทึมที่ 1
ใน Python เราสามารถแสดงอัลกอริทึมนี้เป็น:
def ex1(A):
sum = A[0]
for i in A:
sum = sum + A[i]
return sum
Python Code for the 1st Algorithm in Python
If you use inputs up to about 5 million you will see that this algorithm takes about 4 seconds to run with that large of an input.
Performance of the 1st Algorithm
Then you can compare this algorithm in C++, like this:
double ex1(double A[], size_t n)
{
double sum = A[0];
for(size_t i = 0; i < n; i++)
{
s = s + A[i];
}
return sum;
}
C++ Code for the 1st Algorithm
You may be wondering, what is size_t? size_t is an "unsigned integer". This just means that this variable does not have a sign. Which also just means that this variable is not negative.
In C++, we use size_t to keep track of positions in an array, since those positions cannot be negative (at least in C++, since in Python we can have negative indexes).
Now let's take a look at the running time of the first algorithm:
Performance of the 1st Algorithm in C++
As you can see from these two charts, the time complexity seems to be linear. This means that the time complexity of this algorithm is O(n) – as the input size for A grows, the time it takes for this algorithm to run grows linearly.
But it is interesting to notice that for very large inputs of 5 million, C++ does not even break the 1 second mark. Whereas Python breaks the 3-4 second mark for inputs at about 5 million. Let's move on to our next algorithm.
Algorithm Ex3(A):
Input: An array "A" storing n >= 1 integers.
Output: The sum of the prefix sums in A.
s = 0
for i = 0 to n - 1 do:
s = s + A[0]
for j = 1 to i do:
s = s + A[j]
return s
Pseudo-Code for the 2nd Algorithm
Here, you can see that this algorithm has two for-loops. So, moving forward this is something to acknowledge when analyzing the time complexity.
In Python, you can express this algorithm like this:
def ex3(A):
sum = 0
for i in range(len(A)):
sum = sum + A[i]
for j in range(1, i):
sum = sum + A[j]
return sum
Python Code for 2nd Algorithm
ลองใช้อินพุตไปจนถึง 70,000 จากนั้นเราจะบันทึกเวลาและจัดทำแผนภูมิข้อมูลดังนี้:
ประสิทธิภาพเวลาของอัลกอริทึมที่ 2 ใน Python
ผลลัพธ์เหล่านี้แตกต่างอย่างมากจากแผนภูมิก่อนหน้าของเราจากอัลกอริทึม 1 สิ่งที่จะชี้ให้เห็นก็คือด้วยอินพุต 70,000 อัลกอริทึมนี้ใช้เวลาถึง 91 วินาทีใน Python นั่นเป็นเวลานาน!
นอกจากนี้ เมื่อฉันพยายามเรียกใช้อินพุตที่สูงกว่า 70,000 คอมพิวเตอร์ของฉันเริ่มไม่ตอบสนอง ใช่
มาดูอัลกอริทึมนี้ใน C ++:
double ex3(double A[], size_t n)
{
double s = 0;
for(size_t i = 0; i < n; i++)
{
s = s + A[i];
for(size_t j = 1; j < i; j++)
{
s = s + A[j];
}
}
return s;
}
รหัส C ++ สำหรับอัลกอริทึมที่ 2
สำหรับโค้ด C++ ฉันสามารถเพิ่มขนาดอินพุตเป็น ~90,000
ประสิทธิภาพเวลาของอัลกอริทึมที่ 2 ใน C ++
For an input size of 70,000 this algorithm takes ~4 seconds to run in C++. That difference is huge. Not to mention, I was able to use inputs that were about ~90,000 elements in size for C++, with it not even breaking past the 10 second mark.
Also, we can see that the curve is not as smooth as the Python graph. This could be due to some other processes running in the background (since I'm running on Windows 10) or some other reason.
Also, we can classify the time complexity of this algorithm as O(n^2), which just means that the time complexity for this algorithm is quadratic.
Let's move on to the last algorithm.
Algorithm Ex5(A, B):
Input: Arrays "A" and "B" each storing n >= 1 integers.
Output: The number of elements in B equal to the sum of
prefix sums in A.
c = 0
for i = 0 to n - 1 do:
s = 0
for j = 0 to n - 1 do:
s = s + A[0]
for k = 1 to j do:
s = s + A[k]
if B[i] == s then:
c = c + 1
return c
Pseudo-Code for the 3rd Algorithm
Let's take a look at this code in Python:
def ex3(A, B):
c = 0
for i in range(len(A)):
s = 0
for j in range(len(A)):
s = s + A[0]
for k in range(1, j):
s = s + A[k]
if B[i] == s:
c = c + 1
return c
Python Code for the 3rd Algorithm
Now, let's analyze the timings:
ประสิทธิภาพเวลาของอัลกอริทึมที่ 3 ใน Python
ว้าว เกิดอะไรขึ้นที่นี่? เราจะเห็นว่าเมื่ออินพุตของเราเริ่มเพิ่มขึ้น อัตราที่อัลกอริทึมทำงานก็เริ่มเพิ่มขึ้นเช่นกัน แต่มันเพิ่มขึ้นอย่างรวดเร็ว
ในกรณีนี้ เราสามารถเห็นได้ด้วยขนาดอินพุต 3500 ว่าอัลกอริทึมนี้ใช้เวลา 761 วินาทีในการทำงานใน Python คุณอาจจะถามว่า "คุณนั่งครบ 761 วินาทีจริงหรือเปล่า" คำตอบคือใช่ ใช่ฉันทำ.
ทีนี้มาดูรหัส C ++:
double ex5(double A[], double B[], size_t n)
{
double c = 0;
for(size_t i = 0; i < n; i++)
{
double s = 0;
for(size_t j = 0; j < n; i++)
{
s = s + A[0]
for(size_t k = 1; k < j; k++)
{
s = s + A[k];
}
if(B[i] == s)
{
c = c + 1
}
}
}
return c;
}
รหัส C ++ สำหรับอัลกอริทึมที่ 3
ลองมาดูกราฟกัน
ประสิทธิภาพเวลาของอัลกอริทึมที่ 3 ใน C ++
คล้ายกับอัลกอริทึมที่สอง เป็นเรื่องที่น่าสนใจที่จะเห็นว่าเส้นโค้งนั้นไม่เรียบเหมือนกราฟของ Python
นอกจากนี้ เราจะเห็นว่าเราสามารถไปได้ดีกับขนาดอินพุตที่ 3500 คอมพิวเตอร์ของฉันเริ่มทำงานเมื่อฉันดันขนาดอินพุตเกิน 10,000 สำหรับ C++ ไม่ต้องพูดถึง ด้วยขนาดอินพุต 10,000 อัลกอริทึมเฉลี่ยประมาณ 544-545 วินาที
เราสามารถจำแนกอัลกอริทึมนี้ว่ามีความซับซ้อนของเวลาเท่ากับO(n!) ซึ่งหมายความว่าอัลกอริทึมนี้เป็นแฟกทอเรียล ซึ่งทำงานช้ามาก
ฉันหวังว่าคุณจะพบความแตกต่างของเวลาทำงานระหว่าง Python และ C++ ที่น่าสนใจเช่นเดียวกับฉัน
นอกจากนี้ การที่ Python ทำงานช้ากว่า C++ สำหรับทุกๆ อัลกอริทึม ไม่ได้หมายความว่า C++ เป็นภาษาที่ "ดีกว่า" ทั้งสองภาษานี้มีจุดประสงค์เฉพาะสำหรับประเภทของซอฟต์แวร์ที่คุณกำลังพยายามสร้าง
C ++ จะเป็นภาษาที่ต้องการหากประสิทธิภาพมีความสำคัญ หากคุณกำลังเขียนโปรแกรมเกม ระบบปฏิบัติการ หรือสื่อสารระหว่างเครื่องจักร C++ น่าจะเป็นตัวเลือกที่ดีกว่าเนื่องจากคอมไพล์เร็วและเป็นธรรมชาติ
Python จะดีกว่าหากคุณต้องการพัฒนาซอฟต์แวร์อย่างรวดเร็ว เนื่องจากเส้นโค้งการเรียนรู้ที่ง่ายกว่า เกือบทุกคนสามารถรับ Python และเริ่มสร้างซอฟต์แวร์ด้วยมันได้ Python ยังมีแหล่งข้อมูลมากมายสำหรับวิทยาการข้อมูลและการเรียนรู้ของเครื่อง
ที่มา: https://www.freecodecamp.org
#python #cplusplus #cpp #datastructures #algorithms
1677754160
Análisis de complejidad de tiempo de Python vs C++. Las diferencias de tiempos de ejecución entre Python y C++. En este tutorial, verá 3 algoritmos y cómo funcionan en C++ y Python. ¿Cuánto más rápido es C++ en comparación con Python?
La velocidad es importante en los lenguajes de programación y algunos se ejecutan mucho más rápido que otros.
Por ejemplo, es posible que sepa que C++ es más rápido que Python. Entonces porqué es este el caso?
Bueno, C++ es un lenguaje que usa un compilador, sin mencionar que es un lenguaje de programación de mucho más bajo nivel que Python. Es decir, C++ proporciona mucha menos abstracción del conjunto de instrucciones y la arquitectura de una computadora.
Por otro lado, Python es un lenguaje interpretado, lo que significa que cada línea del programa se evalúa a medida que se ejecuta el programa.
Pero, ¿cuánto más rápido es C++ en comparación con Python? En este artículo, verá 3 algoritmos y cómo funcionan en C++ y Python. Estos algoritmos son de "Estructuras de datos y algoritmos usando C++" de Michael T. Goodrich.
Nota: La computadora en la que realicé estas pruebas era una Acer Swift 3. La CPU era una Ryzen 7 7500U con Radeon Graphics 1.80Gz. Junto con 8 GB de RAM, con Windows 10 Home.
Verá estos algoritmos y su notación Big O. Big O es una forma matemática de expresar el peor de los casos de la complejidad de tiempo o espacio de un algoritmo.
La complejidad del tiempo es la cantidad de tiempo que tarda en ejecutarse un algoritmo, mientras que la complejidad del espacio es la cantidad de espacio (memoria) que ocupa un algoritmo. Aquí hay un gráfico para ayudar a explicar Big O:
Big O Cheat Sheet – Tabla de complejidad de tiempo (freecodecamp.org)
De ahora en adelante, me referiré a cada algoritmo como si se ejecutara con cierta complejidad de tiempo.
Por ejemplo, si digo que un algoritmo se ejecuta con una complejidad de tiempo O(n) , esto significa que a medida que crece la entrada, el tiempo que tarda un algoritmo en ejecutarse es lineal . Si digo que se ejecuta con una complejidad de tiempo O(n^2) , esto significa que a medida que crece la entrada, el tiempo que tarda un algoritmo en ejecutarse es cuadrático .
Algorithm Ex1(A):
Input: An array "A" storing n >= 1 integers.
Output: The sum of the elements in A.
s = A[0]
for i = 1 to n - 1 do:
s = s + A[i]
return s
Pseudo-Código para el 1er Algoritmo
En Python, podemos expresar este algoritmo como:
def ex1(A):
sum = A[0]
for i in A:
sum = sum + A[i]
return sum
Código de Python para el primer algoritmo en Python
Si usa entradas de hasta aproximadamente 5 millones, verá que este algoritmo tarda aproximadamente 4 segundos en ejecutarse con una entrada tan grande.
Desempeño del 1er Algoritmo
Luego puedes comparar este algoritmo en C++, así:
double ex1(double A[], size_t n)
{
double sum = A[0];
for(size_t i = 0; i < n; i++)
{
s = s + A[i];
}
return sum;
}
Código C++ para el 1er Algoritmo
Te estarás preguntando, ¿qué es size_t? size_tes un "entero sin signo". Esto solo significa que esta variable no tiene signo. Lo que también significa que esta variable no es negativa .
En C++, usamos size_tpara realizar un seguimiento de las posiciones en una matriz, ya que esas posiciones no pueden ser negativas (al menos en C++, ya que en Python podemos tener índices negativos).
Ahora echemos un vistazo al tiempo de ejecución del primer algoritmo:
Desempeño del 1er Algoritmo en C++
Como puede ver en estos dos gráficos, la complejidad del tiempo parece ser lineal. Esto significa que la complejidad temporal de este algoritmo es O(n) : a medida que crece el tamaño de entrada A, el tiempo que tarda este algoritmo en ejecutarse crece linealmente.
Pero es interesante notar que para entradas muy grandes de 5 millones, C++ ni siquiera supera la marca de 1 segundo. Mientras que Python rompe la marca de 3-4 segundos para entradas en alrededor de 5 millones. Pasemos a nuestro siguiente algoritmo.
Algorithm Ex3(A):
Input: An array "A" storing n >= 1 integers.
Output: The sum of the prefix sums in A.
s = 0
for i = 0 to n - 1 do:
s = s + A[0]
for j = 1 to i do:
s = s + A[j]
return s
Pseudocódigo para el segundo algoritmo
Aquí puede ver que este algoritmo tiene dos bucles for. Entonces, seguir adelante es algo a reconocer cuando se analiza la complejidad del tiempo.
En Python, puedes expresar este algoritmo así:
def ex3(A):
sum = 0
for i in range(len(A)):
sum = sum + A[i]
for j in range(1, i):
sum = sum + A[j]
return sum
Código de Python para el segundo algoritmo
También usemos entradas hasta 70,000. Luego registraremos los tiempos y graficaremos los datos así:
Desempeño en el tiempo del segundo algoritmo en Python
Estos resultados son muy diferentes de nuestros gráficos anteriores del algoritmo 1. Algo para señalar es que con una entrada de 70,000, este algoritmo tarda la friolera de 91 segundos en Python. ¡Eso es un largo tiempo!
Además, cuando traté de ejecutar entradas superiores a 70,000, mi computadora comenzó a dejar de responder. ¡Ay!
Echemos un vistazo a este algoritmo en C++:
double ex3(double A[], size_t n)
{
double s = 0;
for(size_t i = 0; i < n; i++)
{
s = s + A[i];
for(size_t j = 1; j < i; j++)
{
s = s + A[j];
}
}
return s;
}
Código C++ para el segundo algoritmo
Para el código C++, pude aumentar el tamaño de entrada a ~90 000.
Desempeño en el tiempo del segundo algoritmo en C++
Para un tamaño de entrada de 70 000, este algoritmo tarda unos 4 segundos en ejecutarse en C++. Esa diferencia es enorme. Sin mencionar que pude usar entradas que tenían un tamaño de ~ 90,000 elementos para C ++, y ni siquiera superó la marca de 10 segundos.
Además, podemos ver que la curva no es tan suave como el gráfico de Python. Esto podría deberse a otros procesos que se ejecutan en segundo plano (ya que estoy ejecutando Windows 10) o alguna otra razón.
Además, podemos clasificar la complejidad temporal de este algoritmo como O(n^2) , lo que significa que la complejidad temporal de este algoritmo es cuadrática.
Pasemos al último algoritmo.
Algorithm Ex5(A, B):
Input: Arrays "A" and "B" each storing n >= 1 integers.
Output: The number of elements in B equal to the sum of
prefix sums in A.
c = 0
for i = 0 to n - 1 do:
s = 0
for j = 0 to n - 1 do:
s = s + A[0]
for k = 1 to j do:
s = s + A[k]
if B[i] == s then:
c = c + 1
return c
Pseudo-Código para el 3er Algoritmo
Echemos un vistazo a este código en Python:
def ex3(A, B):
c = 0
for i in range(len(A)):
s = 0
for j in range(len(A)):
s = s + A[0]
for k in range(1, j):
s = s + A[k]
if B[i] == s:
c = c + 1
return c
Código de Python para el tercer algoritmo
Ahora, analicemos los tiempos:
Desempeño en el tiempo del tercer algoritmo en Python
Vaya, ¿qué está pasando aquí? Podemos ver que a medida que nuestras entradas comienzan a aumentar, la velocidad a la que se ejecuta el algoritmo también comienza a aumentar, pero aumenta a una velocidad drástica.
En este caso, podemos ver con un tamaño de entrada de 3500 que este algoritmo tarda 761 segundos en ejecutarse en Python. Es posible que se pregunte: "¿Realmente se sentó durante los 761 segundos?", La respuesta es sí. Sí, lo hice.
Ahora echemos un vistazo al código C++:
double ex5(double A[], double B[], size_t n)
{
double c = 0;
for(size_t i = 0; i < n; i++)
{
double s = 0;
for(size_t j = 0; j < n; i++)
{
s = s + A[0]
for(size_t k = 1; k < j; k++)
{
s = s + A[k];
}
if(B[i] == s)
{
c = c + 1
}
}
}
return c;
}
Código C++ para el tercer algoritmo
Echemos un vistazo a la gráfica.
Desempeño en el tiempo del tercer algoritmo en C++
Similar al segundo algoritmo, es interesante ver que la curva no es tan suave como el gráfico de Python.
Además, podemos ver que podemos ir muy por encima de un tamaño de entrada de 3500. Mi computadora comenzó a fallar una vez que empujé el tamaño de entrada más allá de 10,000 para C++. Sin mencionar que con un tamaño de entrada de 10,000, el algoritmo promedió alrededor de 544-545 segundos.
Podemos clasificar este algoritmo con una complejidad temporal de O(n!). Lo que solo significa que este algoritmo es factorial, que se ejecuta muy lentamente.
Espero que hayas encontrado las diferencias de los tiempos de ejecución entre Python y C++ tan fascinantes como yo.
Además, el hecho de que Python se ejecute más lento que C++ para todos los algoritmos no significa que C++ sea el "mejor" lenguaje. Ambos lenguajes tienen sus propios propósitos para el tipo de software que intenta crear.
C++ sería el lenguaje preferido si el rendimiento es crítico. Si estuviera programando juegos, sistemas operativos o comunicándose entre máquinas, C++ sería la mejor opción debido a su naturaleza compilada y rápida.
Python sería preferible si necesita desarrollar software rápidamente. Debido a su curva de aprendizaje más fácil, casi cualquier persona puede elegir Python y comenzar a crear software con él. Python también proporciona muchos recursos para la ciencia de datos y el aprendizaje automático.
Fuente: https://www.freecodecamp.org
#python #cplusplus #cpp #datastructures #algorithms
1677750540
Analyse de la complexité temporelle Python vs C++. Les différences de temps d'exécution entre Python et C++. Dans ce didacticiel, vous verrez 3 algorithmes et leurs performances en C++ et Python. À quel point C++ est-il plus rapide que Python ?
La vitesse est importante dans les langages de programmation, et certains s'exécutent beaucoup plus rapidement que d'autres.
Par exemple, vous savez peut-être que C++ est plus rapide que Python. Alors pourquoi est-ce le cas?
Eh bien, C++ est un langage qui utilise un compilateur, sans oublier qu'il s'agit d'un langage de programmation de niveau bien inférieur à Python. C'est-à-dire que C++ fournit beaucoup moins d'abstraction à partir du jeu d'instructions et de l'architecture d'un ordinateur.
D'autre part, Python est un langage interprété, ce qui signifie simplement que chaque ligne du programme est évaluée pendant que le programme s'exécute.
Mais, à quel point le C++ est-il plus rapide que Python ? Dans cet article, vous verrez 3 algorithmes et leurs performances en C++ et Python. Ces algorithmes proviennent de "Data Structures and Algorithms Using C++" de Michael T. Goodrich.
Remarque : L'ordinateur sur lequel j'ai effectué ces tests était un Acer Swift 3. Le processeur était un Ryzen 7 7500U avec Radeon Graphics 1.80Gz. Avec 8 Go de RAM, exécutant Windows 10 Home.
Vous examinerez ces algorithmes et leur notation Big O . Big O est une manière mathématique d'exprimer le pire scénario de la complexité temporelle ou spatiale d'un algorithme.
La complexité temporelle est le temps nécessaire à l'exécution d'un algorithme, tandis que la complexité spatiale est la quantité d'espace (mémoire) qu'un algorithme occupe. Voici un graphique pour aider à expliquer Big O :
Feuille de triche Big O - Tableau de complexité temporelle (freecodecamp.org)
À partir de maintenant, je désignerai chaque algorithme comme s'exécutant avec une certaine complexité temporelle.
Par exemple, si je dis qu'un algorithme s'exécute avec une complexité temporelle O(n) , cela signifie qu'à mesure que l'entrée augmente, le temps nécessaire à l'exécution d'un algorithme est linéaire . Si je dis qu'il s'exécute avec une complexité temporelle O(n^2) , cela signifie qu'à mesure que l'entrée augmente, le temps nécessaire à l'exécution d'un algorithme est quadratique .
Algorithm Ex1(A):
Input: An array "A" storing n >= 1 integers.
Output: The sum of the elements in A.
s = A[0]
for i = 1 to n - 1 do:
s = s + A[i]
return s
Pseudo-code pour le 1er algorithme
En Python, nous pouvons exprimer cet algorithme comme suit :
def ex1(A):
sum = A[0]
for i in A:
sum = sum + A[i]
return sum
Python Code for the 1st Algorithm in Python
If you use inputs up to about 5 million you will see that this algorithm takes about 4 seconds to run with that large of an input.
Performance of the 1st Algorithm
Then you can compare this algorithm in C++, like this:
double ex1(double A[], size_t n)
{
double sum = A[0];
for(size_t i = 0; i < n; i++)
{
s = s + A[i];
}
return sum;
}
C++ Code for the 1st Algorithm
You may be wondering, what is size_t? size_t is an "unsigned integer". This just means that this variable does not have a sign. Which also just means that this variable is not negative.
In C++, we use size_t to keep track of positions in an array, since those positions cannot be negative (at least in C++, since in Python we can have negative indexes).
Now let's take a look at the running time of the first algorithm:
Performance of the 1st Algorithm in C++
Comme vous pouvez le voir sur ces deux graphiques, la complexité temporelle semble être linéaire. Cela signifie que la complexité temporelle de cet algorithme est O (n) - à mesure que la taille d'entrée pour Aaugmente, le temps nécessaire à l'exécution de cet algorithme augmente de manière linéaire.
Mais il est intéressant de noter que pour de très grandes entrées de 5 millions, C++ ne dépasse même pas la barre des 1 seconde. Alors que Python dépasse la barre des 3-4 secondes pour les entrées à environ 5 millions. Passons à notre prochain algorithme.
Algorithm Ex3(A):
Input: An array "A" storing n >= 1 integers.
Output: The sum of the prefix sums in A.
s = 0
for i = 0 to n - 1 do:
s = s + A[0]
for j = 1 to i do:
s = s + A[j]
return s
Pseudo-code pour le 2e algorithme
Ici, vous pouvez voir que cet algorithme a deux boucles for. Donc, aller de l'avant est quelque chose à reconnaître lors de l'analyse de la complexité temporelle.
En Python, vous pouvez exprimer cet algorithme comme ceci :
def ex3(A):
sum = 0
for i in range(len(A)):
sum = sum + A[i]
for j in range(1, i):
sum = sum + A[j]
return sum
Code Python pour le 2e algorithme
Utilisons également des entrées jusqu'à 70 000. Ensuite, nous enregistrerons les temps et tracerons les données comme suit :
Performances temporelles du 2e algorithme en Python
Ces résultats sont très différents de nos graphiques précédents de l'algorithme 1. Il convient de souligner qu'avec une entrée de 70 000, cet algorithme prend 91 secondes en Python. C'est une longue période!
De plus, lorsque j'ai essayé d'exécuter des entrées supérieures à 70 000, mon ordinateur a commencé à ne plus répondre. Ouais.
Jetons un coup d'œil à cet algorithme en C++ :
double ex3(double A[], size_t n)
{
double s = 0;
for(size_t i = 0; i < n; i++)
{
s = s + A[i];
for(size_t j = 1; j < i; j++)
{
s = s + A[j];
}
}
return s;
}
Code C++ pour le 2e algorithme
Pour le code C++, j'ai pu augmenter la taille d'entrée à ~ 90 000.
Performances temporelles du 2e algorithme en C++
Pour une taille d'entrée de 70 000, cet algorithme prend environ 4 secondes pour s'exécuter en C++. Cette différence est énorme. Sans oublier que j'ai pu utiliser des entrées d'une taille d'environ 90 000 éléments pour C++, sans même dépasser la barre des 10 secondes.
De plus, nous pouvons voir que la courbe n'est pas aussi lisse que le graphique Python. Cela peut être dû à d'autres processus exécutés en arrière-plan (puisque je suis sous Windows 10) ou à une autre raison.
De plus, nous pouvons classer la complexité temporelle de cet algorithme comme O(n^2) , ce qui signifie simplement que la complexité temporelle de cet algorithme est quadratique.
Passons au dernier algorithme.
Algorithm Ex5(A, B):
Input: Arrays "A" and "B" each storing n >= 1 integers.
Output: The number of elements in B equal to the sum of
prefix sums in A.
c = 0
for i = 0 to n - 1 do:
s = 0
for j = 0 to n - 1 do:
s = s + A[0]
for k = 1 to j do:
s = s + A[k]
if B[i] == s then:
c = c + 1
return c
Pseudo-code pour le 3e algorithme
Jetons un coup d'œil à ce code en Python :
def ex3(A, B):
c = 0
for i in range(len(A)):
s = 0
for j in range(len(A)):
s = s + A[0]
for k in range(1, j):
s = s + A[k]
if B[i] == s:
c = c + 1
return c
Code Python pour le 3ème algorithme
Maintenant, analysons les timings :
Performances temporelles du 3ème algorithme en Python
Waouh, qu'est-ce qui se passe ici ? Nous pouvons voir qu'à mesure que nos entrées commencent à augmenter, la vitesse à laquelle l'algorithme s'exécute commence également à augmenter, mais elle augmente à un rythme drastique.
Dans ce cas, nous pouvons voir avec une taille d'entrée de 3500 qu'il faut 761 secondes pour que cet algorithme s'exécute en Python. Vous vous demandez peut-être : « Avez-vous réellement passé les 761 secondes ? », la réponse est oui. Oui je l'ai fait.
Examinons maintenant le code C++ :
double ex5(double A[], double B[], size_t n)
{
double c = 0;
for(size_t i = 0; i < n; i++)
{
double s = 0;
for(size_t j = 0; j < n; i++)
{
s = s + A[0]
for(size_t k = 1; k < j; k++)
{
s = s + A[k];
}
if(B[i] == s)
{
c = c + 1
}
}
}
return c;
}
Code C++ pour le 3ème algorithme
Jetons un coup d'œil au graphique.
Performances temporelles du 3ème algorithme en C++
Semblable au deuxième algorithme, il est intéressant de voir que la courbe n'est pas aussi lisse que le graphe Python.
De plus, nous pouvons voir que nous pouvons aller bien au-delà d'une taille d'entrée de 3500. Mon ordinateur a commencé à agir une fois que j'ai poussé la taille d'entrée au-delà de 10 000 pour C++. Sans oublier qu'avec une taille d'entrée de 10 000, l'algorithme prenait en moyenne environ 544 à 545 secondes.
On peut classer cet algorithme comme ayant une complexité temporelle de O(n!). Ce qui signifie simplement que cet algorithme est factoriel, qui s'exécute très lentement.
J'espère que vous avez trouvé les différences de temps d'exécution entre Python et C++ aussi fascinantes que moi.
De plus, ce n'est pas parce que Python s'exécute plus lentement que C++ pour chaque algorithme que C++ est le "meilleur" langage. Ces deux langages ont leurs propres objectifs pour le type de logiciel que vous essayez de créer.
C++ serait le langage préféré si les performances sont critiques. Si vous programmiez des jeux, des systèmes d'exploitation ou communiquiez entre des machines, C++ serait le meilleur choix en raison de sa nature compilée et rapide.
Python serait préférable si vous avez besoin de développer rapidement des logiciels. En raison de sa courbe d'apprentissage plus facile, presque tout le monde peut prendre Python et commencer à créer des logiciels avec. Python fournit également de nombreuses ressources pour la science des données et l'apprentissage automatique.
Source : https://www.freecodecamp.org
#python #cplusplus #cpp #datastructures #algorithms