20221207比对python和C的运行效率(以六元一次方程组为例)【大概300倍】

news2024/9/22 5:36:56

20221207比对python和C的运行效率(以六元一次方程组为例)
2022/12/7 17:30

C:\20221207比对python和C的运行效率(以六元一次方程组为例)\1000-1000-1000-1000-1000-1000

(只跑一次)
Python源码:

# http://datachart.500.com/ssq/?expect=100
# https://zst.cjcp.com.cn/shdd/ssq-hq.html

import numpy as np
import time
#print(time.time())
time1 = time.time()

m = np.array([[10,13,16,20,21,25],
    [5,10,13,18,24,26],
    [2,3,7,12,20,31],
    [1,13,15,17,26,33],
    [3,4,9,10,29,33],
    [3,12,18,24,27,29]])

print(m)

n = np.array([1000, 1000, 1000, 1000, 1000, 1000])

print(n)

solution = np.linalg.solve(m, n)

print(solution)

for x1 in range(1, 29):
    for x2 in range(x1+1, 30):
        for x3 in range(x2+1, 31):
            for x4 in range(x3+1, 32):
                for x5 in range(x4+1, 33):
                    for x6 in range(x5+1, 34):
                        #if( 999.99 < x1*solution[0] + x2*solution[1] + x3*solution[2] + x4*solution[3] + x5*solution[4] + x6*solution[5] < 1000.01 ):
                        if( 999 < x1*solution[0] + x2*solution[1] + x3*solution[2] + x4*solution[3] + x5*solution[4] + x6*solution[5] < 1001 ):
                            #print(x1, x2, x3, x4, x5, x6)
                            array1 = [x1, x2, x3, x4, x5, x6]
                            array2 = [10,12,14,22,24,27]
                            len1 = len(set(array1) & set(array2))
                            #if(len1>3):
                            if(len1>4):
                                print(x1, x2, x3, x4, x5, x6)
                                print(set(array1) & set(array2))
                                print("****")

#print(time.time())
time2 = time.time()
print(str(time1))
print(str(time2))
print(str(time2-time1))
 

C源码:

//#include<iostream>
//using namespace std;
#include <stdio.h>
#include <time.h>

//#define N 4//我自己假设的例子,这里N等于4,结果a=1,b=2,c=3,d=4
//double m[N][N + 1] = {
//    0,3,2,1, 16,
//    1,1,1,1, 10,
//    3,2,2,1, 17,
//    2,0,1,2, 13
不放心你就自己整几个例子啊,学生也很忙的啊,现在都1:53了,肝死我了
//};

#define N 6
double m[N][N + 1] = {
    10,13,16,20,21,25,1000,
    5,10,13,18,24,26,1000,
    2,3,7,12,20,31,1000,
    1,13,15,17,26,33,1000,
    3,4,9,10,29,33,1000,
    3,12,18,24,27,29,1000
//不放心你就自己整几个例子啊,学生也很忙的啊,现在都1:53了,肝死我了
};

double s[N];
int loc[N];

void Allmin(int s, int n);//声明函数,不然会报错

void make1(int h, int n)
{
    double t = m[h][n];
    if (m[h][n] == 0)
        return;
    for (int i = 0; i < N + 1; i++)
        m[h][i] /= t;
}

void setloc(int h, int n)
{
    loc[h] = n;
}

void Allmake1(int n)
{
    for (int i = 0; i < N; i++)
    {
        if (loc[i] == -1 && m[i][n] != 0)
            make1(i, n);
    }
}

void Onereplace(int n)
{
    int l;
    for (int i = 0; i < N; i++)
    {
        if (loc[i] == -1 && m[i][n] != 0)
        {
            l = i;
            setloc(i, n);
            make1(i, n);
            break;
        }    
    }
    Allmake1(n);
    Allmin(l,n);
}

void min(int s,int h,int n)
{
    if(loc[h]==-1&&m[h][n]!=0)
        for (int i = 0; i < N+1; i++)
            m[h][i] -= m[s][i];
}

void Allmin(int s,int n)
{
    for (int i = 0; i < N; i++)
        min(s, i, n);
}

void Allreplace()
{
    for (int i = 0; i < N; i++)
        Onereplace(i);
}

void cacular(int n)
{
    for(int i=0;i<N;i++)
        if (loc[i] == n)
        {
            for (int k = 0; k < N; k++)
                if (m[i][k]!=0&&k != n)
                    m[i][N] -= m[i][k] * s[k];
            s[n] = m[i][N] / m[i][n];
            break;
        }
}

void Allcacular()
{
    for (int i = N - 1; i >= 0; i--)
        cacular(i);
}

void pc()
{
    //fill(loc, loc + N, -1);
    int i = 0;
    for(i=0;i<N;i++)
    {
        loc[i] = -1;
    }
    
    for(i=0;i<N;i++)
    {
        //loc[i] = -1;
        printf("%d ", loc[i] );
    }
    
    printf("\n\n");
    
    Allreplace();
    Allcacular();
}

void Cout()
{
    for (int i = 0; i < N; i++)
        printf("%.8lf\n", s[i] );


    int x1 = 0;
    int x2 = 0;
    int x3 = 0;
    int x4 = 0;
    int x5 = 0;
    int x6 = 0;
    double temp0 = 0.0;
    int v[6] = {10,12,14,22,24,27};
    
    
    for(x1=1;x1<29;x1++)
        for(x2=x1+1;x2<30;x2++)
            for(x3=x2+1;x3<31;x3++)
                for(x4=x3+1;x4<32;x4++)
                    for(x5=x4+1;x5<33;x5++)
                        for(x6=x5+1;x6<34;x6++)
                        {
                            temp0 = x1*s[0] + x2*s[1] + x3*s[2] + x4*s[3] + x5*s[4] + x6*s[5];
                            if( 999 < temp0 && temp0 < 1001 )
                            //if( 999 < temp )
                            //if( temp < 1001 )
                            {
                                //printf("x1=%02d, %02d, %02d, %02d, %02d, %02d, %.8lf\n", x1, x2, x3, x4, x5, x6, temp );
                                int temp = 0;
                                for(int i=0;i<6;i++)
                                {
                                    if(x1==v[i])
                                    {
                                        temp++;
                                    }
                                    
                                    if(x2==v[i])
                                    {
                                        temp++;
                                    }
                                    
                                    if(x3==v[i])
                                    {
                                        temp++;
                                    }
                                    
                                    if(x4==v[i])
                                    {
                                        temp++;
                                    }
                                    
                                    if(x5==v[i])
                                    {
                                        temp++;
                                    }
                                    
                                    if(x6==v[i])
                                    {
                                        temp++;
                                    }
                                }
                                
                                //if(temp>=4)
                                if(temp>=5)
                                {
                                    //cout << x1 << " " << x2 << " " << x3 << " " << x4 << " " << x5 << " " << x6 << " "<< temp << "=temp";
                                    //cout << endl;
                                    //printf("x1=%02d, %02d, %02d, %02d, %02d, %02d, %.8lf\n", x1, x2, x3, x4, x5, x6, temp );
                                    printf("x1=%02d, %02d, %02d, %02d, %02d, %02d, %.8lf, %02d\n", x1, x2, x3, x4, x5, x6, temp0, temp );
                                }
                            }
                        }
}

int main()
{
    clock_t begin = clock();
    
    pc();
    Cout();
    
    clock_t end = clock();
    double time_consumption = (double)(end - begin) / CLOCKS_PER_SEC;
    printf("time cost=%fs!!!!\n", time_consumption);
}

进行结果:

rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ 
rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ python shuangseqiu31.py 
[[10 13 16 20 21 25]
 [ 5 10 13 18 24 26]
 [ 2  3  7 12 20 31]
 [ 1 13 15 17 26 33]
 [ 3  4  9 10 29 33]
 [ 3 12 18 24 27 29]]
[1000 1000 1000 1000 1000 1000]
[ 21.20379105  23.53008801 -62.96555525  34.91136065  11.98313435
  21.5858718 ]
10 12 18 22 24 27
{10, 12, 22, 24, 27}
****
1670406045.1046996
1670406046.466995
1.362295389175415
rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ 
rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ 
rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ gcc lottery14.c -o lottery14
rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ 
rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ ./lottery14
-1 -1 -1 -1 -1 -1 

21.20379105
23.53008801
-62.96555525
34.91136065
11.98313435
21.58587180
x1=10, 12, 18, 22, 24, 27, 999.48266943, 05
time cost=0.007543s!!!!
rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ 
rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ 

速度(时间比对):Python/C=1.362295389175415/0.007543=180.60392273305249900570064960891
也就是跑同样的算法,C的速度/效率大概是Python的180倍!

 

参考效率比对:

rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ 
rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ 
rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ python shuangseqiu980a.py 
[10, 12, 18, 22, 24, 27]
{10, 12, 22, 24, 27}
****5
1000 1000 1000 1000 1000 1000

1670407708.8548648
1670407710.2211242
cost time = 1.3662593364715576 s

rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ 
rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ 
rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ gcc lottery980a.c -o lottery980a
lottery980a.c:134:13: warning: built-in function ¡®y1¡¯ declared as non-function [-Wbuiltin-declaration-mismatch]
  134 |         int y1 = 0;
      |             ^~
rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ 
rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ ./lottery980a


10 12 22 24 27 00 
x1=10, 12, 18, 22, 24, 27, 999.48266943, 05
y1=1000, 1000, 1000, 1000, 1000, 1000

time cost=0.004189s!!!!
rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ 
rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ 


1.3662593364715576/0.004189=326.15405501827586536166149439007

 

1000-1000-1000-1000-1000-980
【跑21次】

Python:

# http://datachart.500.com/ssq/?expect=100
# https://zst.cjcp.com.cn/shdd/ssq-hq.html


import numpy as np
import time
time1 = time.time()

m = np.array([[10,13,16,20,21,25],
    [5,10,13,18,24,26],
    [2,3,7,12,20,31],
    [1,13,15,17,26,33],
    [3,4,9,10,29,33],
    [3,12,18,24,27,29]])

#print(m)

for y1 in range(1000, 1001):
    for y2 in range(1000, 1001):
        for y3 in range(1000, 1001):
            for y4 in range(1000, 1001):
                for y5 in range(1000, 1001):
                    for y6 in range(980, 1001):
                    #for y6 in range(1000, 1001):
                        #print(y1, y2, y3, y4, y5, y6)
                        
                        n = np.array([y1, y2, y3, y4, y5, y6])
                        solution = np.linalg.solve(m, n)
                        
                        #print(solution)
                        
                        
                        for x1 in range(1, 29):
                            for x2 in range(x1+1, 30):
                                for x3 in range(x2+1, 31):
                                    for x4 in range(x3+1, 32):
                                        for x5 in range(x4+1, 33):
                                            for x6 in range(x5+1, 34):
                                                #if( 999.99 < x1*solution[0] + x2*solution[1] + x3*solution[2] + x4*solution[3] + x5*solution[4] + x6*solution[5] < 1000.01 ):
                                                if( 999 < x1*solution[0] + x2*solution[1] + x3*solution[2] + x4*solution[3] + x5*solution[4] + x6*solution[5] < 1001 ):
                                                    #print(x1, x2, x3, x4, x5, x6)
                                                    array1 = [x1, x2, x3, x4, x5, x6]
                                                    array2 = [10,12,14,22,24,27]
                                                    len1 = len(set(array1) & set(array2))
                                                    #if(len1>3):
                                                    if(len1>4):
                                                        print(y1, y2, y3, y4, y5, y6)
                                                        print(solution)
                                                        print(array1)
                                                        print(set(array1) & set(array2))
                                                        print("****" + str(len1))
                                                        print()
                        #print()


time2 = time.time()
print(str(time1))
print(str(time2))

print("cost time = " + str(time2-time1) + " s")
print()

C语言:

//#include<iostream>
//using namespace std;
#include <stdio.h>
#include <time.h>

//#define N 4//ÎÒ×Ô¼º¼ÙÉèµÄÀý×Ó£¬ÕâÀïNµÈÓÚ4£¬½á¹ûa=1,b=2,c=3,d=4
//double m[N][N + 1] = {
//    0,3,2,1, 16,
//    1,1,1,1, 10,
//    3,2,2,1, 17,
//    2,0,1,2, 13
²»·ÅÐÄÄã¾Í×Ô¼ºÕû¼¸¸öÀý×Ó°¡£¬Ñ§ÉúÒ²ºÜæµÄ°¡£¬ÏÖÔÚ¶¼1:53ÁË£¬¸ÎËÀÎÒÁË
//};

    
    
#define N 6
double m2[N][N + 1] = {
    10,13,16,20,21,25,1000,
    5,10,13,18,24,26,1000,
    2,3,7,12,20,31,1000,
    1,13,15,17,26,33,1000,
    3,4,9,10,29,33,1000,
    3,12,18,24,27,29,1000
//²»·ÅÐÄÄã¾Í×Ô¼ºÕû¼¸¸öÀý×Ó°¡£¬Ñ§ÉúÒ²ºÜæµÄ°¡£¬ÏÖÔÚ¶¼1:53ÁË£¬¸ÎËÀÎÒÁË
};

double m[N][N + 1] = {
    10,13,16,20,21,25,1000,
    5,10,13,18,24,26,1000,
    2,3,7,12,20,31,1000,
    1,13,15,17,26,33,1000,
    3,4,9,10,29,33,1000,
    3,12,18,24,27,29,1000
//²»·ÅÐÄÄã¾Í×Ô¼ºÕû¼¸¸öÀý×Ó°¡£¬Ñ§ÉúÒ²ºÜæµÄ°¡£¬ÏÖÔÚ¶¼1:53ÁË£¬¸ÎËÀÎÒÁË
};

double s[N];
int loc[N];

void Allmin(int s, int n);//ÉùÃ÷º¯Êý£¬²»È»»á±¨´í

void make1(int h, int n)
{
    double t = m[h][n];
    if (m[h][n] == 0)
        return;
    for (int i = 0; i < N + 1; i++)
        m[h][i] /= t;
}

void setloc(int h, int n)
{
    loc[h] = n;
}

void Allmake1(int n)
{
    for (int i = 0; i < N; i++)
    {
        if (loc[i] == -1 && m[i][n] != 0)
            make1(i, n);
    }
}

void Onereplace(int n)
{
    int l;
    for (int i = 0; i < N; i++)
    {
        if (loc[i] == -1 && m[i][n] != 0)
        {
            l = i;
            setloc(i, n);
            make1(i, n);
            break;
        }    
    }
    Allmake1(n);
    Allmin(l,n);
}

void min(int s,int h,int n)
{
    if(loc[h]==-1&&m[h][n]!=0)
        for (int i = 0; i < N+1; i++)
            m[h][i] -= m[s][i];
}

void Allmin(int s,int n)
{
    for (int i = 0; i < N; i++)
        min(s, i, n);
}

void Allreplace()
{
    for (int i = 0; i < N; i++)
        Onereplace(i);
}

void cacular(int n)
{
    for(int i=0;i<N;i++)
        if (loc[i] == n)
        {
            for (int k = 0; k < N; k++)
                if (m[i][k]!=0&&k != n)
                    m[i][N] -= m[i][k] * s[k];
            s[n] = m[i][N] / m[i][n];
            break;
        }
}

void Allcacular()
{
    for (int i = N - 1; i >= 0; i--)
        cacular(i);
}

void pc()
{
    //fill(loc, loc + N, -1);
    int i = 0;
    for(i=0;i<N;i++)
    {
        loc[i] = -1;
    }
    
    //printf("\n\n");
    
    Allreplace();
    Allcacular();
}

    volatile int y1 = 0;
    volatile int y2 = 0;
    volatile int y3 = 0;
    volatile int y4 = 0;
    volatile int y5 = 0;
    volatile int y6 = 0;
    
void Cout()
{
    int x1 = 0;
    int x2 = 0;
    int x3 = 0;
    int x4 = 0;
    int x5 = 0;
    int x6 = 0;
    double temp0 = 0.0;
    int v[6] = {10,12,14,22,24,27};
    
    
    
    for(x1=1;x1<29;x1++)
        for(x2=x1+1;x2<30;x2++)
            for(x3=x2+1;x3<31;x3++)
                for(x4=x3+1;x4<32;x4++)
                    for(x5=x4+1;x5<33;x5++)
                        for(x6=x5+1;x6<34;x6++)
                        {
                            int t[6] = {0,0,0,0,0,0};
                            
                            temp0 = x1*s[0] + x2*s[1] + x3*s[2] + x4*s[3] + x5*s[4] + x6*s[5];
                            if( 999 < temp0 && temp0 < 1001 )
                            {
                                int temp = 0;
                                for(int i=0;i<6;i++)
                                {
                                    if(x1==v[i])
                                    {
                                        t[temp] = x1;
                                        temp++;
                                    }
                                    
                                    if(x2==v[i])
                                    {
                                        t[temp] = x2;
                                        temp++;
                                    }
                                    
                                    if(x3==v[i])
                                    {
                                        t[temp] = x3;
                                        temp++;
                                    }
                                    
                                    if(x4==v[i])
                                    {
                                        t[temp] = x4;
                                        temp++;
                                    }
                                    
                                    if(x5==v[i])
                                    {
                                        t[temp] = x5;
                                        temp++;
                                    }
                                    
                                    if(x6==v[i])
                                    {
                                        t[temp] = x6;
                                        temp++;
                                    }
                                }
                                
                                //if(temp>=4)
                                if(temp>=5)
                                {
                                    printf("y1=%02d, %02d, %02d, %02d, %02d, %02d\n", y1, y2, y3, y4, y5, y6);
                                    for (int i = 0; i < N; i++)
                                        //printf("%.8lf\n", s[i] );
                                        printf("%.8lf,", s[i] );
                                    printf("\n");
                                    
                                    //cout << x1 << " " << x2 << " " << x3 << " " << x4 << " " << x5 << " " << x6 << " "<< temp << "=temp";
                                    //cout << endl;
                                    //printf("x1=%02d, %02d, %02d, %02d, %02d, %02d, %.8lf\n", x1, x2, x3, x4, x5, x6, temp );
                                    printf("x1=%02d, %02d, %02d, %02d, %02d, %02d, %.8lf, %02d\n", x1, x2, x3, x4, x5, x6, temp0, temp );
                                    //printf("y1=%02d, %02d, %02d, %02d, %02d, %02d\n\n", y1, y2, y3, y4, y5, y6);
                                    
                                    
                                    for (int i = 0; i < N; i++)
                                    {
                                        printf("%02d ", t[i] );
                                    }
                                    printf("\n");
                                    printf("\n");
                                }
                                //printf("\n");
                            }
                        }
}

int main()
{
    clock_t begin = clock();
    
    //pc();
    //Cout();


    //int y1 = 0;
    //int y2 = 0;
    //int y3 = 0;
    //int y4 = 0;
    //int y5 = 0;
    //int y6 = 0;
    for(y1=1000;y1<1001;y1++)
    {
        for(y2=1000;y2<1001;y2++)
        {
            for(y3=1000;y3<1001;y3++)
            {
                for(y4=1000;y4<1001;y4++)
                {
                    for(y5=1000;y5<1001;y5++)
                    {
                        for(y6=980;y6<1001;y6++)
                        //for(y6=1000;y6<1001;y6++)
                        {
                            m[0][6]=y1;
                            m[1][6]=y2;
                            m[2][6]=y3;
                            m[3][6]=y4;
                            m[4][6]=y5;
                            m[5][6]=y6;
                            
                            int l = 0;
                            int n = 0;
                            
                            
                            for(l=0;l<6;l++)
                            {
                                //for(n=0;n<6;n++)
                                //for(n=0;n<7;n++)
                                for(n=0;n<6;n++)
                                {
                                    //printf("%02d,", m[l][n]);
                                    //printf("%.8lf,", m[l][n]);
                                    m[l][n] = m2[l][n];
                                }
                                //printf("\n");
                            }
                            //printf("\n");
                            
                            //for(l=0;l<6;l++)
                            //{
                            //    //for(n=0;n<6;n++)
                            //    for(n=0;n<7;n++)
                            //    {
                            //        //printf("%02d,", m[l][n]);
                            //        printf("%.8lf,", m[l][n]);
                            //        //m[l][n] = m[l][n];
                            //    }
                            //    printf("\n");
                            //}
                            //printf("\n");
                            
                            pc();
                            Cout();
                        }
                    }
                }
            }
        }
    }
    
    
    clock_t end = clock();
    double time_consumption = (double)(end - begin) / CLOCKS_PER_SEC;
    printf("time cost=%fs!!!!\n", time_consumption);
}

rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ 
rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ python shuangseqiu980b4.py 
1000 1000 1000 1000 1000 980
[ 22.53579894  27.91367004 -68.87604016  34.98040568  12.58601043
  21.99466439]
[10, 11, 12, 14, 22, 24]
{10, 12, 14, 22, 24}
****5

1000 1000 1000 1000 1000 981
[ 22.46919854  27.69449094 -68.58051591  34.97695343  12.55586663
  21.97422476]
[10, 11, 12, 14, 22, 24]
{10, 12, 14, 22, 24}
****5

1000 1000 1000 1000 1000 993
[ 21.66999381  25.06434172 -65.03422497  34.93552641  12.19414098
  21.72894921]
[10, 14, 22, 24, 27, 32]
{10, 14, 22, 24, 27}
****5

1000 1000 1000 1000 1000 993
[ 21.66999381  25.06434172 -65.03422497  34.93552641  12.19414098
  21.72894921]
[12, 14, 22, 24, 27, 30]
{12, 14, 22, 24, 27}
****5

1000 1000 1000 1000 1000 994
[ 21.60339342  24.84516262 -64.73870072  34.93207416  12.16399717
  21.70850958]
[12, 14, 22, 24, 27, 30]
{12, 14, 22, 24, 27}
****5

1000 1000 1000 1000 1000 1000
[ 21.20379105  23.53008801 -62.96555525  34.91136065  11.98313435
  21.5858718 ]
[10, 12, 18, 22, 24, 27]
{10, 12, 22, 24, 27}
****5

1670408219.945487
1670408246.8223503
cost time = 26.87686324119568 s

rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ 
rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ 
rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ gcc lottery980b9
lottery980b9               lottery980b9.c             lottery980b9 - ¸±±¾ (2).c  lottery980b9 - ¸±±¾.c      
rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ gcc lottery980b9.c -o lottery980b9
lottery980b9.c:138:22: warning: built-in function ¡®y1¡¯ declared as non-function [-Wbuiltin-declaration-mismatch]
  138 |         volatile int y1 = 0;
      |                      ^~
rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ 
rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ ./lottery980b9
y1=1000, 1000, 1000, 1000, 1000, 980
22.53579894,27.91367004,-68.87604016,34.98040568,12.58601043,21.99466439,
x1=10, 11, 12, 14, 22, 24, 1000.38573250, 05
10 12 14 22 24 00 

y1=1000, 1000, 1000, 1000, 1000, 981
22.46919854,27.69449094,-68.58051591,34.97695343,12.55586663,21.97422476,
x1=10, 11, 12, 14, 22, 24, 999.65300305, 05
10 12 14 22 24 00 

y1=1000, 1000, 1000, 1000, 1000, 993
21.66999381,25.06434172,-65.03422497,34.93552641,12.19414098,21.72894921,
x1=10, 14, 22, 24, 27, 32, 999.86858789, 05
10 14 22 24 27 00 

y1=1000, 1000, 1000, 1000, 1000, 993
21.66999381,25.06434172,-65.03422497,34.93552641,12.19414098,21.72894921,
x1=12, 14, 22, 24, 27, 30, 999.75067709, 05
12 14 22 24 27 00 

y1=1000, 1000, 1000, 1000, 1000, 994
21.60339342,24.84516262,-64.73870072,34.93207416,12.16399717,21.70850958,
x1=12, 14, 22, 24, 27, 30, 1000.87457270, 05
12 14 22 24 27 00 

y1=1000, 1000, 1000, 1000, 1000, 1000
21.20379105,23.53008801,-62.96555525,34.91136065,11.98313435,21.58587180,
x1=10, 12, 18, 22, 24, 27, 999.48266943, 05
10 12 22 24 27 00 

time cost=0.087094s!!!!
rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ 
rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ 
rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ 


效率比对(时间对比):Python/c=26.87686324119568/0.087094=308.59603693934920890072794911245

 

 

C:\20221207比对python和C的运行效率(以六元一次方程组为例)\1000-1000-1000-1000-980-980

循环(21*21=441)次

Python源码:

# http://datachart.500.com/ssq/?expect=100
# https://zst.cjcp.com.cn/shdd/ssq-hq.html


import numpy as np
import time
time1 = time.time()

m = np.array([[10,13,16,20,21,25],
    [5,10,13,18,24,26],
    [2,3,7,12,20,31],
    [1,13,15,17,26,33],
    [3,4,9,10,29,33],
    [3,12,18,24,27,29]])

#print(m)

for y1 in range(1000, 1001):
    for y2 in range(1000, 1001):
        for y3 in range(1000, 1001):
            for y4 in range(1000, 1001):
                #for y5 in range(1000, 1001):
                for y5 in range(980, 1001):
                    for y6 in range(980, 1001):
                    #for y6 in range(1000, 1001):
                        #print(y1, y2, y3, y4, y5, y6)
                        
                        n = np.array([y1, y2, y3, y4, y5, y6])
                        solution = np.linalg.solve(m, n)
                        
                        #print(solution)
                        
                        
                        for x1 in range(1, 29):
                            for x2 in range(x1+1, 30):
                                for x3 in range(x2+1, 31):
                                    for x4 in range(x3+1, 32):
                                        for x5 in range(x4+1, 33):
                                            for x6 in range(x5+1, 34):
                                                #if( 999.99 < x1*solution[0] + x2*solution[1] + x3*solution[2] + x4*solution[3] + x5*solution[4] + x6*solution[5] < 1000.01 ):
                                                if( 999 < x1*solution[0] + x2*solution[1] + x3*solution[2] + x4*solution[3] + x5*solution[4] + x6*solution[5] < 1001 ):
                                                    #print(x1, x2, x3, x4, x5, x6)
                                                    array1 = [x1, x2, x3, x4, x5, x6]
                                                    array2 = [10,12,14,22,24,27]
                                                    len1 = len(set(array1) & set(array2))
                                                    #if(len1>3):
                                                    if(len1>4):
                                                        print(y1, y2, y3, y4, y5, y6)
                                                        print(solution)
                                                        print(array1)
                                                        print(set(array1) & set(array2))
                                                        print("****" + str(len1))
                                                        print()
                        #print()


time2 = time.time()
print(str(time1))
print(str(time2))

print("cost time = " + str(time2-time1) + " s")
print()

C源码:

//#include<iostream>
//using namespace std;
#include <stdio.h>
#include <time.h>

//#define N 4//ÎÒ×Ô¼º¼ÙÉèµÄÀý×Ó£¬ÕâÀïNµÈÓÚ4£¬½á¹ûa=1,b=2,c=3,d=4
//double m[N][N + 1] = {
//    0,3,2,1, 16,
//    1,1,1,1, 10,
//    3,2,2,1, 17,
//    2,0,1,2, 13
²»·ÅÐÄÄã¾Í×Ô¼ºÕû¼¸¸öÀý×Ó°¡£¬Ñ§ÉúÒ²ºÜæµÄ°¡£¬ÏÖÔÚ¶¼1:53ÁË£¬¸ÎËÀÎÒÁË
//};

    
    
#define N 6
double m2[N][N + 1] = {
    10,13,16,20,21,25,1000,
    5,10,13,18,24,26,1000,
    2,3,7,12,20,31,1000,
    1,13,15,17,26,33,1000,
    3,4,9,10,29,33,1000,
    3,12,18,24,27,29,1000
//²»·ÅÐÄÄã¾Í×Ô¼ºÕû¼¸¸öÀý×Ó°¡£¬Ñ§ÉúÒ²ºÜæµÄ°¡£¬ÏÖÔÚ¶¼1:53ÁË£¬¸ÎËÀÎÒÁË
};

double m[N][N + 1] = {
    10,13,16,20,21,25,1000,
    5,10,13,18,24,26,1000,
    2,3,7,12,20,31,1000,
    1,13,15,17,26,33,1000,
    3,4,9,10,29,33,1000,
    3,12,18,24,27,29,1000
//²»·ÅÐÄÄã¾Í×Ô¼ºÕû¼¸¸öÀý×Ó°¡£¬Ñ§ÉúÒ²ºÜæµÄ°¡£¬ÏÖÔÚ¶¼1:53ÁË£¬¸ÎËÀÎÒÁË
};

double s[N];
int loc[N];

void Allmin(int s, int n);//ÉùÃ÷º¯Êý£¬²»È»»á±¨´í

void make1(int h, int n)
{
    double t = m[h][n];
    if (m[h][n] == 0)
        return;
    for (int i = 0; i < N + 1; i++)
        m[h][i] /= t;
}

void setloc(int h, int n)
{
    loc[h] = n;
}

void Allmake1(int n)
{
    for (int i = 0; i < N; i++)
    {
        if (loc[i] == -1 && m[i][n] != 0)
            make1(i, n);
    }
}

void Onereplace(int n)
{
    int l;
    for (int i = 0; i < N; i++)
    {
        if (loc[i] == -1 && m[i][n] != 0)
        {
            l = i;
            setloc(i, n);
            make1(i, n);
            break;
        }    
    }
    Allmake1(n);
    Allmin(l,n);
}

void min(int s,int h,int n)
{
    if(loc[h]==-1&&m[h][n]!=0)
        for (int i = 0; i < N+1; i++)
            m[h][i] -= m[s][i];
}

void Allmin(int s,int n)
{
    for (int i = 0; i < N; i++)
        min(s, i, n);
}

void Allreplace()
{
    for (int i = 0; i < N; i++)
        Onereplace(i);
}

void cacular(int n)
{
    for(int i=0;i<N;i++)
        if (loc[i] == n)
        {
            for (int k = 0; k < N; k++)
                if (m[i][k]!=0&&k != n)
                    m[i][N] -= m[i][k] * s[k];
            s[n] = m[i][N] / m[i][n];
            break;
        }
}

void Allcacular()
{
    for (int i = N - 1; i >= 0; i--)
        cacular(i);
}

void pc()
{
    //fill(loc, loc + N, -1);
    int i = 0;
    for(i=0;i<N;i++)
    {
        loc[i] = -1;
    }
    
    //printf("\n\n");
    
    Allreplace();
    Allcacular();
}

    volatile int y1 = 0;
    volatile int y2 = 0;
    volatile int y3 = 0;
    volatile int y4 = 0;
    volatile int y5 = 0;
    volatile int y6 = 0;
    
void Cout()
{
    int x1 = 0;
    int x2 = 0;
    int x3 = 0;
    int x4 = 0;
    int x5 = 0;
    int x6 = 0;
    double temp0 = 0.0;
    int v[6] = {10,12,14,22,24,27};
    
    
    
    for(x1=1;x1<29;x1++)
        for(x2=x1+1;x2<30;x2++)
            for(x3=x2+1;x3<31;x3++)
                for(x4=x3+1;x4<32;x4++)
                    for(x5=x4+1;x5<33;x5++)
                        for(x6=x5+1;x6<34;x6++)
                        {
                            int t[6] = {0,0,0,0,0,0};
                            
                            temp0 = x1*s[0] + x2*s[1] + x3*s[2] + x4*s[3] + x5*s[4] + x6*s[5];
                            if( 999 < temp0 && temp0 < 1001 )
                            {
                                int temp = 0;
                                for(int i=0;i<6;i++)
                                {
                                    if(x1==v[i])
                                    {
                                        t[temp] = x1;
                                        temp++;
                                    }
                                    
                                    if(x2==v[i])
                                    {
                                        t[temp] = x2;
                                        temp++;
                                    }
                                    
                                    if(x3==v[i])
                                    {
                                        t[temp] = x3;
                                        temp++;
                                    }
                                    
                                    if(x4==v[i])
                                    {
                                        t[temp] = x4;
                                        temp++;
                                    }
                                    
                                    if(x5==v[i])
                                    {
                                        t[temp] = x5;
                                        temp++;
                                    }
                                    
                                    if(x6==v[i])
                                    {
                                        t[temp] = x6;
                                        temp++;
                                    }
                                }
                                
                                //if(temp>=4)
                                if(temp>=5)
                                {
                                    printf("y1=%02d, %02d, %02d, %02d, %02d, %02d\n", y1, y2, y3, y4, y5, y6);
                                    for (int i = 0; i < N; i++)
                                        //printf("%.8lf\n", s[i] );
                                        printf("%.8lf,", s[i] );
                                    printf("\n");
                                    
                                    //cout << x1 << " " << x2 << " " << x3 << " " << x4 << " " << x5 << " " << x6 << " "<< temp << "=temp";
                                    //cout << endl;
                                    //printf("x1=%02d, %02d, %02d, %02d, %02d, %02d, %.8lf\n", x1, x2, x3, x4, x5, x6, temp );
                                    printf("x1=%02d, %02d, %02d, %02d, %02d, %02d, %.8lf, %02d\n", x1, x2, x3, x4, x5, x6, temp0, temp );
                                    //printf("y1=%02d, %02d, %02d, %02d, %02d, %02d\n\n", y1, y2, y3, y4, y5, y6);
                                    
                                    
                                    for (int i = 0; i < N; i++)
                                    {
                                        printf("%02d ", t[i] );
                                    }
                                    printf("\n");
                                    printf("\n");
                                }
                                //printf("\n");
                            }
                        }
}

int main()
{
    clock_t begin = clock();
    
    //pc();
    //Cout();


    //int y1 = 0;
    //int y2 = 0;
    //int y3 = 0;
    //int y4 = 0;
    //int y5 = 0;
    //int y6 = 0;
    for(y1=1000;y1<1001;y1++)
    {
        for(y2=1000;y2<1001;y2++)
        {
            for(y3=1000;y3<1001;y3++)
            {
                for(y4=1000;y4<1001;y4++)
                {
                    //for(y5=1000;y5<1001;y5++)
                    for(y5=980;y5<1001;y5++)
                    {
                        for(y6=980;y6<1001;y6++)
                        //for(y6=1000;y6<1001;y6++)
                        {
                            m[0][6]=y1;
                            m[1][6]=y2;
                            m[2][6]=y3;
                            m[3][6]=y4;
                            m[4][6]=y5;
                            m[5][6]=y6;
                            
                            int l = 0;
                            int n = 0;
                            
                            
                            for(l=0;l<6;l++)
                            {
                                //for(n=0;n<6;n++)
                                //for(n=0;n<7;n++)
                                for(n=0;n<6;n++)
                                {
                                    //printf("%02d,", m[l][n]);
                                    //printf("%.8lf,", m[l][n]);
                                    m[l][n] = m2[l][n];
                                }
                                //printf("\n");
                            }
                            //printf("\n");
                            
                            //for(l=0;l<6;l++)
                            //{
                            //    //for(n=0;n<6;n++)
                            //    for(n=0;n<7;n++)
                            //    {
                            //        //printf("%02d,", m[l][n]);
                            //        printf("%.8lf,", m[l][n]);
                            //        //m[l][n] = m[l][n];
                            //    }
                            //    printf("\n");
                            //}
                            //printf("\n");
                            
                            pc();
                            Cout();
                        }
                    }
                }
            }
        }
    }
    
    
    clock_t end = clock();
    double time_consumption = (double)(end - begin) / CLOCKS_PER_SEC;
    printf("time cost=%fs!!!!\n", time_consumption);
}

【版面原因,就不会贴完整的结果了】

C的效果图:

y1=1000, 1000, 1000, 1000, 1000, 1000
21.20379105,23.53008801,-62.96555525,34.91136065,11.98313435,21.58587180,
x1=10, 12, 18, 22, 24, 27, 999.48266943, 05
10 12 22 24 27 00 

time cost=1.815098s!!!!
rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ 
 

python的效果图:


1000 1000 1000 1000 1000 1000
[ 21.20379105  23.53008801 -62.96555525  34.91136065  11.98313435
  21.5858718 ]
[10, 12, 18, 22, 24, 27]
{10, 12, 22, 24, 27}
****5

1670409116.466532
1670409706.6580608
cost time = 590.1915287971497 s

rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ 
rootroot@rootroot-adol-ADOLBOOK-I421UAY-ADOL14UA:~/wyb$ 
 

效率比对(时间对比):Python/c=590.1915287971497/1.815098=325.1568393536600778580550471655

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/69468.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【web前端期末大作业】基于html关爱空巢老人网页设计与实现

&#x1f389;精彩专栏推荐 &#x1f4ad;文末获取联系 ✍️ 作者简介: 一个热爱把逻辑思维转变为代码的技术博主 &#x1f482; 作者主页: 【主页——&#x1f680;获取更多优质源码】 &#x1f393; web前端期末大作业&#xff1a; 【&#x1f4da;毕设项目精品实战案例 (10…

程序设计基础最后一次作业——解析

文章目录打开项目文件完成题目initialize函数从文件info.txt中读入人员信息数据PrintAllSearchbyNameInsertSendMessage问题为什么txt打开是乱码打开项目文件 解压文件列表如下&#xff0c;每一个框是一组&#xff0c;.h后缀的文件是对应cpp文件的头文件&#xff0c;声明了相关…

理解路由表 实验报告

实验名称&#xff1a; 理解路由表 一、实验预习 1、实验目的 利用route命令查看、增加、删除等主机路由信息&#xff0c;以进一步熟悉和理解路由表。 实验内容&#xff08;包括自己设计增加的内容等&#xff09; 利用route命令查看、增加、…

制造业数据安全如何保证?天翎知识文档管理系统+群晖NAS企业级解决方案

编者按&#xff1a;本文分析了制造业文档管理中的需求&#xff0c;并进一步指出天翎知识文档管理系统群晖NAS企业级解决方案是如何满足企业需求&#xff0c;为企业赋能创新的。 关键词&#xff1a;免安装维护&#xff0c;流程审批&#xff0c;私有化部署&#xff0c;安全技术&…

基于人工蜂群算法的新型概率密度模型的无人机路径规划(Matlab代码实现)

目录 1 概述 1.1研究背景 2 运行结果 3 Matlab代码实现 4 结语 5 参考文献 1 概述 文章提出一种基于人工蜂群算法的非确定性双向规划机制搜索算法。首先针对不同场景&#xff0c;采用两种方法进行环境建模&#xff1b;其次&#xff0c;结合非确定性搜索机制引入双向规划方…

数字后端:track的作用与创建

1.track的定义 EDA工具根据LEF(或tech file)中每层金属的pitch大小&#xff0c;在芯片内部自动生成各层金属连续的、规则的绕线轨道&#xff08;routing track&#xff09;&#xff0c;routing track指的就是绕线的路径。 tech lef示例 tech file示例 DEF中保存的TRACK信息如下…

射频工程师自学HFSS、CST和ADS打开方式

导读&#xff1a;大家好&#xff0c;我是RF小木匠,受仿真秀平台邀请&#xff0c;近日正式入驻了仿真秀App,今天RF小木匠想和大家聊一下电磁仿真软件学习那些事。 一、仿真是射频工程师的必备技能 当我们开始接触电磁波和微波工程的时候&#xff0c;第一件事就是仿真。电磁仿真…

Docker 安装 Python

文章目录使用docker pull python:3.8 命令安装使用python镜像&#xff08;一&#xff09;新建py文件&#xff08;二&#xff09;运行容器使用docker pull python:3.8 命令安装 查找Docker Hub上的python镜像&#xff0c;输入命令&#xff1a;docker search python 这里我拉取…

【图像去噪】中值+均值+维纳+小波滤波图像去噪【含GUI Matlab源码 753期】

⛄一、图像去噪及滤波简介 1 图像去噪 1.1 图像噪声定义 噪声是干扰图像视觉效果的重要因素&#xff0c;图像去噪是指减少图像中噪声的过程。噪声分类有三种&#xff1a;加性噪声&#xff0c;乘性噪声和量化噪声。我们用f(x,y&#xff09;表示图像&#xff0c;g(x,y&#xff0…

HDFS的概念及shell操作

HDFS 产出背景及定义 产生背景 随着数据量越来越大&#xff0c;在一个操作系统存不下所有的数据&#xff0c;那么就分配到更多的操作系 统管理的磁盘中&#xff0c;但是不方便管理和维护&#xff0c;迫切需要一种系统来管理多台机器上的文件&#xff0c;这 就是分布式文件管…

虚拟机共享物理机网路的NAT连接方法

NAT模式指借助虚拟NAT设备和虚拟DHCP服务器&#xff0c;使得虚拟机可以联网。电脑连接wifi&#xff0c;让笔记本虚拟机也能够上网&#xff0c;其配置方法如下&#xff1a; 1、打开VMware Workstation软件&#xff0c;点击编辑虚拟机设置&#xff0c;进入到配置虚拟机参数菜单。…

加密技术和二维码

1、对称加密&#xff1a;单密钥加密。一个密钥可以用来加密也可以用来解密 —- AES 2、非对称加密&#xff1a;有两把密码&#xff0c;公钥(用于加密)&#xff0c;私钥(用于解密) 3、摘要算法&#xff1a;把任意长度的输入&#xff0c;根据算法生成一串固定长度的伪随机数 —…

【安全扫描处理问题心得】

安全扫描处理问题心得 在安全扫描中处理了一些问题&#xff0c;现在来总结一下 1.mysql漏洞&#xff08;CVE-2022-21595 CVE-2022-21617 CVE-2022-21608 CVE-2022-2097 CVE-2022-21592 CVE-2022-21589&#xff09; 解决过程 我们公司安扫文档中的 解决办法 如下&#xff1a; 目…

JAVA SCRIPT设计模式--创建型设计模式之原型模式(4)

JAVA SCRIPT设计模式是本人根据GOF的设计模式写的博客记录。使用JAVA SCRIPT语言来实现主体功能&#xff0c;所以不可能像C&#xff0c;JAVA等面向对象语言一样严谨&#xff0c;大部分程序都附上了JAVA SCRIPT代码&#xff0c;代码只是实现了设计模式的主体功能&#xff0c;不代…

Sunday 算法详解

Sunday 算法 Sunday算法是Daniel M.Sunday于1990年提出的字符串模式匹配。其核心思想是&#xff1a;在匹配过程中&#xff0c;模式串发现不匹配时&#xff0c;算法能跳过尽可能多的字符以进行下一步的匹配&#xff0c;从而提高了匹配效率。 一、匹配机制 匹配机制非常容易理…

PyTorch笔记 - 开发技巧与爱因斯坦标示法(einops)

欢迎关注我的CSDN:https://blog.csdn.net/caroline_wendy 本文地址:https://blog.csdn.net/caroline_wendy/article/details/128222398 NLP seq2seq代码编写技巧 数据 tokenization,离散符号,翻译,划分tokentoken2idx,将token变成idxadd SoS&EoS,开头和结尾添加标识…

代码随想录算法训练营第五十七天| LeetCode647. 回文子串、LeetCode516. 最长回文子序列

一、LeetCode647. 回文子串 1&#xff1a;题目描述&#xff08;647. 回文子串&#xff09; 给你一个字符串 s &#xff0c;请你统计并返回这个字符串中 回文子串 的数目。 回文字符串 是正着读和倒过来读一样的字符串。 子字符串 是字符串中的由连续字符组成的一个序列。 具有不…

Unity3d C#获取海康/大华摄像头在萤石平台直播视频流功能(含源码)

前言 实现该功能主要是因为之前硬件实施工程是给我的视频流地址总是失效&#xff0c;原因就是视频流地址其实都有一个过期时间&#xff08;类似于&#xff1a;rtmp://rtmp01open.ys7.com:/v3/openlive/_1_1?expire1669341172&id***&t8cb15956b1ca09770d1496bff3b8ccb…

电子书小程序源码

资源简介 价值8k的的电子书资料文库文集ip打造流量主小程序系统源码 个人即可搭建&#xff0c;附上此系统源码以及详细的‘’一小时的小白手把手教搭建教程‘’ 下面开始系统介绍&#xff1a; 无意中在群里刷到一个文集小程序 点击立即学习里面&#xff0c;是电子书。 点击了…

虹科分享|基于CAN/CAN FD技术的机器狗是什么样的?

机器狗是一种结合了仿生学和人工智能的仿生四足机器人&#xff0c;其外形与四足动物相似&#xff0c;由于其运动摆脱了轮式和履带式的设计&#xff0c;机器狗的机动性更为强大&#xff0c;能够灵活地完成多种复杂的运动&#xff0c;并能够在不同的地理环境中自主行走&#xff0…