Delay in execution of code in C

Asked

Viewed 93 times

-1

Hello guys this is the first time I’m looking for help, well I’m a freshman in computer science and I’m having a hard time at a job where the teacher asked me to fix code for a random person in the room, I ended up taking the code from a guy where he had to concatenate alternately two Ldses. I was able to fix this code since it previously only concatenated, but one thing I found strange is that at the time of executing the code it took a long time to appear the menu and also sometimes only appeared when I press some key, I can’t find the problem that could be causing this, so I’d like a little help. That is the code:


 #include < stdio.h>

 #include < stdlib.h>

 #include < string.h>

 #include < locale.h>

 #define max 9

typedef struct dadosLDSE
{
    int vlr;
    struct dadosLDSE* prox;
}sLDSE;

sLDSE* lista_insere (sLDSE* l, int i)
{
    sLDSE *aux, *auxIns;

    aux = (sLDSE *) malloc(sizeof(sLDSE));

    if (aux == NULL){
        exit (1);
    }

    aux->vlr = i;
    aux->prox = NULL;
    if(l == NULL){
        l = aux;
    }else{
        auxIns = l;
        while (auxIns->prox != NULL){
            auxIns = auxIns->prox;
        }
        auxIns->prox = aux;
    }
    return l;
}

void lista_imprime (sLDSE* l)
{
    /* nao precisa de variavel auxiliar para percorrer a lista */
    for ( ;l != NULL; l = l->prox)
        printf("%d\n", l->vlr);
}


/* concatena modificando lista */
void concatena (sLDSE* l1, sLDSE* l2)
{
    int p = 0;
    while (l1 != NULL || l2 != NULL){
        if (p%2 == 0 && l1 != NULL){
            printf ("%d \n", l1->vlr);
            l1 = l1->prox;
        }else{
            if (l1 == NULL && l2 != NULL){
                printf ("%d \n", l2->vlr);
                l2 = l2->prox;
            }
        }

        if (p%2 != 0 && l2 != NULL){
            printf ("%d \n", l2->vlr);
            l2 = l2->prox;
        }else{
            if (l2 == NULL && l1 != NULL){
                printf ("%d \n", l1->vlr);
                l1 = l1->prox;
            }
        }
        p++;
    }
}

int main()
{
    setlocale(LC_ALL, "Portuguese");
    int vlr;
    int resp;

    //criando lista l
    sLDSE *l;
    l = NULL;
    //criando lista l2
    sLDSE *l2;
    l2 = NULL;

//Aqui está o menu
    do{
        printf("(1) Inserir no inicio da Lista 1:\n");
        printf("(2) Inserir no inicio da Lista 2:\n");
        printf("(3) Visualizar a Lista concatenada:\n");
        printf("(4) Sair\n\n");
        printf("Digite sua opcao: ");
        scanf("%d", &resp);
        switch(resp){
            case 1: {
                    printf("Digite o valor que será inserido: ");
                    scanf("%d", &vlr);
                    l = lista_insere(l, vlr);
                    break;
            }
            case 2: {
                    printf("Digite o valor que será inserido: ");
                    scanf("%d", &vlr);
                    l2 = lista_insere(l2, vlr);
                    break;
            }
            case 3: {
                    system("cls");
                    printf("\nLista 1:\n");
                    lista_imprime(l); //imprime lista l
                    printf("\n\n");
                    printf("\nLista 2:\n");
                    lista_imprime(l2); //imprime lista l2
                    printf("\n");
                    printf("\nLista concatenada:\n");
                    concatena(l,l2); //chama a função concatenar que envia as duas lista e lá faz printa as duas listas concatenadas
                    break;
            }
        }
        printf("\n\n");
}while(resp != 4);
    return 0;
}

1 answer

0


In theory, the menu should appear almost automatically. And I tested your program and found no problem at startup.

Do the following, check how long, the program is taking since the beginning of função main() until the menu view. If the interval is short, the problem will not be in your program (And where will it be? Maybe, on your computer, compiler, IDE, hard to know).

Now if time is long, test without the setLocale, although I don’t really believe that the setLocale could produce a noticeable delay in that case.

To measure the range, you can use the type clock_t present at header time.h. Check out:


int main()
{
    // inserir essa linha no início da função e não se esquecer de incluir no cabeçalho
    // #include <time.h>
    clock_t t0 = clock(); 
    setlocale(LC_ALL, "Portuguese");
    int vlr;
    int resp;
    //criando lista l
    sLDSE *l;
    l = NULL;
    //criando lista l2
    sLDSE *l2;
    l2 = NULL;

//Aqui está o menu
    do{
        printf("(1) Inserir no inicio da Lista 1:\n");
        printf("(2) Inserir no inicio da Lista 2:\n");
        printf("(3) Visualizar a Lista concatenada:\n");
        printf("(4) Sair\n\n");
        printf("Digite sua opcao: ");
        clock_t t1 = clock();
        //E calcular o intervalo aqui:
        printf("\nTempo inicializacao: %f segundos: ", ((double)(t1 - t0)/CLOCKS_PER_SEC));
        scanf("%d", &resp);
        switch(resp){
            case 1: {
                    printf("Digite o valor que será inserido: ");
                    scanf("%d", &vlr);
                    l = lista_insere(l, vlr);
                    break;
            }
            case 2: {
                    printf("Digite o valor que será inserido: ");
                    scanf("%d", &vlr);
                    l2 = lista_insere(l2, vlr);
                    break;
            }
            case 3: {
                    system("cls");
                    printf("\nLista 1:\n");
                    lista_imprime(l); //imprime lista l
                    printf("\n\n");
                    printf("\nLista 2:\n");
                    lista_imprime(l2); //imprime lista l2
                    printf("\n");
                    printf("\nLista concatenada:\n");
                    concatena(l,l2); //chama a função concatenar que envia as duas lista e lá faz printa as duas listas concatenadas
                    break;
            }
        }
        printf("\n\n");
}while(resp != 4);
    return 0;
}
  • They say that the IDE I’m using is very buggy, but since I’ve been using it since the beginning of the course I’m afraid of trying to migrate to another and have problems to adapt I don’t know in this final stretch of semester, maybe in the holidays while giving a study I test others, but they fight for the tip!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.