C与python的简单单词列表生成器

C与python的简单单词列表生成器,python,c,algorithm,performance,python-3.x,Python,C,Algorithm,Performance,Python 3.x,不久前我用C写了一个单词列表生成器,花了大约2天的时间。我使用一个专用的int数组来存储索引,并使用数字基的思想对它们进行递增。以下是我的源代码: /* * Generates word lists from a character set */ #include <stdio.h> #include <string.h> #include <stdlib.h> int power(int x, int y); void ntoarray(int n,

不久前我用C写了一个单词列表生成器,花了大约2天的时间。我使用一个专用的int数组来存储索引,并使用数字基的思想对它们进行递增。以下是我的源代码:

/*
 * Generates word lists from a character set
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int power(int x, int y);
void ntoarray(int n, int base, int seq[], int len);
void setchar(char charset[], char word[], int indices[]);

int main(int argc, char *argv[])
{
    // checks arguments
    if (argc != 4)
    {
        printf("Usage: %s start_length end_length charset", argv[0]);
        return 1;
    }
    else
    {
        // loops from start length to end length
        for (int lenword = atoi(argv[1]), lim = atoi(argv[2]); lenword <= lim; lenword++)
        {
            // pointer to character set
            char *charset = argv[3];
            // length of the character set
            int lencharset = strlen(charset);
            // array for the word
            char word[lenword + 1];
            word[lenword] = '\0';
            // array for storing the indices to generate the word from the character set
            int indices[lenword];
            // number to convert to required base
            int n = 0;
            // repetition is allowed so the number of times to loop is number^choice
            for (int i = 0; i < power(lencharset, lenword); i++)
            {
                // converts number to an integer array with length of the charset as the base
                ntoarray(n++, lencharset, indices, lenword);
                // sets the word according to the indices array
                setchar(charset, word, indices);
                // prints the word
                printf("%s\n", word);
            }
        }
        return 0;
    }
}

// simple power algorithm which raises x to the power y
int power(int x, int y)
{
    int n = 1;
    for (int i = 0; i < y; i++)
        n *= x;
    return n;
}

// converts n to the required base and stores it in an integer array
void ntoarray(int n, int base, int seq[], int len)
{
    int i = len - 1;
    memset(seq, 0, sizeof(int) * len);
    while (i >= 0 && n >= base)
    {
        int r = n % base;
        n /= base;
        seq[i--] = r;
    }
    seq[i] = n;
}

// sets the word by combining the data from the indices array and the charset array
void setchar(char charset[], char word[], int indices[])
{
    int len = strlen(word);
    for (int i = 0; i < len; i++)
    {
        word[i] = charset[indices[i]];
    }
}
/*
*从字符集生成单词列表
*/
#包括
#包括
#包括
整数幂(整数x,整数y);
空数组(int n,int base,int seq[],int len);
void setchar(char charset[],char word[],int index[]);
int main(int argc,char*argv[])
{
//检查参数
如果(argc!=4)
{
printf(“用法:%s start_length end_length字符集”,argv[0]);
返回1;
}
其他的
{
//从起始长度到结束长度的循环
对于(int lenword=atoi(argv[1]),lim=atoi(argv[2]);lenword=0&&n>=base)
{
int r=n%基数;
n/=基数;
seq[i--]=r;
}
seq[i]=n;
}
//通过组合索引数组和字符集数组中的数据来设置单词
void setchar(字符集[],字符字[],整数索引[])
{
int len=strlen(字);
对于(int i=0;i
现在我用类似的想法在python3中重写了它,但只花了大约一个小时

"""
    Generates word lists from a character set.
"""

import argparse

def main():
    # checks arguments
    parser = argparse.ArgumentParser()
    parser.add_argument("start_length", help = "starting length for your words", type = int)
    parser.add_argument("end_length", help = "ending length for your words", type = int)
    parser.add_argument("charset", help = "character set to be used", type = str)
    args = parser.parse_args()

    charset = args.charset
    len_charset = len(charset)
    # loops from start_length to end_length
    for length in range(args.start_length, args.end_length + 1):
        # initializes indices list
        indices = [0] * length
        # prints the word
        print(genword(charset, indices))
        # increments the indices list and prints the word until the limit
        # repetition is allowed so the number of loops is base ^ length - 1 (-1 for the printed word)
        for i in range(len_charset ** length - 1):
            inc_seq(indices, len_charset)
            print(genword(charset, indices))

def inc_seq(seq, base, index=-1):
    """
        Increments a number sequence with a specified base by one.
    """
    if seq[index] < base - 1:
        seq[index] += 1
    else:
        inc_seq(seq, base, index - 1)
        seq[index] = 0

def genword(charset, indices):
    """
        Generates a word by combining a character set and a list of indices.
    """
    return "".join([charset[i] for i in indices])

if __name__ == "__main__":
    main()
“”“
从字符集生成单词列表。
"""
导入argparse
def main():
#检查参数
parser=argparse.ArgumentParser()
parser.add_参数(“start_length”,help=“单词的起始长度”,type=int)
parser.add_参数(“end_length”,help=“结束单词的长度”,type=int)
parser.add_参数(“charset”,help=“要使用的字符集”,type=str)
args=parser.parse_args()
charset=args.charset
len_charset=len(charset)
#从起始长度到结束长度的循环
对于范围内的长度(args.start_长度,args.end_长度+1):
#初始化索引列表
索引=[0]*长度
#打印单词
打印(简体字(字符集、索引))
#递增索引列表并打印单词,直到达到限制
#允许重复,因此循环数为base^length-1(-1表示打印字)
对于范围内的i(len_字符集**长度-1):
公司序号(指数,长度字符集)
打印(简体字(字符集、索引))
def inc_seq(seq,base,index=-1):
"""
将具有指定基数的数字序列递增1。
"""
如果seq[index]
有一个显著的区别:在C中,我增加了一个中间数n,并用它来修改int数组;在python中,我利用负索引的威力直接增加int列表


我主要通过自学(即阅读书籍和使用在线资源)学习编码,但我还不知道如何正确地分析算法。所以我的问题是:在时间和空间方面,哪个版本更有效?

Huh?我还没有仔细研究过您的代码,但通常C会比Python快得多,因为C被编译成CPU可以直接执行的机器代码,而Python被编译成byt运行在虚拟机上的ecode.C使用CPU的本机数据类型,而Python中的所有内容都是对象,这些对象占用的RAM比本机数据类型多得多。因此Python速度较慢,使用的RAM也更多。但正如您所注意到的,编写和调试Python通常比C快得多。看起来:“我没有驾驶执照,也没有驾驶经验。通常,我坐公共汽车上班,需要半个小时。今天我租了一辆车,花了我两个小时。怎么了?”如果我理解正确,这应该是开着的,你要做的是:
来自itertools进口链,产品;链。来自iterable(产品(charset,repeat=r)表示r在范围内(开始长度,结束长度+1))
。对吗?搜索关键字“profiling”"关于C和python,我想找到一些工具来帮助您评估程序的性能和资源影响。嗯?我没有仔细研究您的代码,但一般来说C会比python快得多,因为C被编译成CPU可以直接执行的机器代码,而python被编译成运行on virtual machine.C使用CPU的本机数据类型,而Python中的所有内容都是一个对象,这些对象占用的RAM比本机数据类型多得多。因此Python速度较慢,使用的RAM也更多。但正如您所注意到的,编写和调试Python通常比C快得多。如下所示:“我没有驾驶执照,也没有驾驶经验。通常,我坐公共汽车上班,需要半个小时。今天我租了一辆车,花了我两个小时。怎么了?”如果我理解正确,这应该是开着的,你要做的是:
来自itertools进口链,产品;链。来自iterable(产品(charset,repeat=r)表示r在范围内(start_length,end_length+1))
。对吗?搜索与C和python相关的关键字“profiling”,以找到有助于评估程序性能和资源影响的工具。