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

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

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值