A fundamental and strange question: Does the algorithm perform addition, multiplication, division performance without distinction?

Source: Internet
Author: User

A fundamental and strange question: Does the algorithm perform addition, multiplication, division performance without distinction? The analysis of computer theory thinks: The calculation performance of addition, multiplication and division is reduced, but to what extent?

Write C program with 30次百万 data calculation to test the time difference between the opposite sex, code such as the following:

#include <stdio.h> #include <stdlib.h> #include <time.h> #define N 1000000void Add (float x[], long N) { float sum = 0;for (Long i = 0; i < n; i++) sum + = X[i];} void prod (float x[], long n) {float sum = 1;for (Long i = 0; i < n; i++) sum *= (X[i]);} void Div (float x[], long N) {for (Long i = 0; i < n; i++) {x[i]/= 3.0;}} int main () {float x[n];clock_t t1 = clock (); for (int i = 0; i <; i++) Add (x, N), clock_t t2 = Clock ();p rintf ("Million data addition time:% F seconds \ n ", (double) (T2-T1)/clocks_per_sec); t1 = clock (); for (int i = 0; i <; i++) prod (x, n) t2 = clock ();p rintf (" Million data Multiplication time:%f s \ n ", (double) (T2-T1)/clocks_per_sec), T1 = Clock (), for (int i = 0; i <; i++) div (x, n), t2 = Clock ();p rintf ( "Million Data division Time:%f sec \ n", (double) (T2-T1)/clocks_per_sec); return 0;}

The results are as follows:

Million data addition time: 0.157051 seconds
Million data multiplication time: 0.184712 seconds
Million Data division time: 0.161014 seconds
-----------------------------------------
Million data addition time: 0.156099 seconds
Million data multiplication time: 0.184023 seconds
Million Data division time: 0.159588 seconds


What is the problem?

Should we say that the basic operations in the algorithm are no different?

A fundamental and strange question: Does the algorithm perform addition, multiplication, division performance without distinction?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.