Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define size 5
- //lĂŞ array
- int readVet (int tab[], int tam) {
- int c;
- for (c = 0; c < tam; c++) {
- printf("Vet[%d] > ", c);
- scanf("%d", &tab[c]);
- }
- return 0;
- }
- //faz soma da array
- int sumVet (int tab[], int tam) {
- int c;
- int sum = 0;
- for (c = 0; c < tam; c++) {
- sum += tab[c];
- }
- return sum;
- }
- //bubble sort da array
- int bSort (int tab[], int tam) {
- int c;
- int aux;
- int troca = 1;
- while (troca != 0) {
- troca=0;
- for (c = 0; c < tam; c++) {
- if (tab[c] > tab[c+1]) {
- aux = tab[c];
- tab[c] = tab[c+1];
- tab[c+1] = aux;
- troca=1;
- }
- }
- }
- return 0;
- }
- //imprime array
- int impressao (int tab[], int tam) {
- int c;
- for (c = 0; c < tam; c++) {
- printf("%d ", tab[c]);
- }
- }
- //fatorial
- int fat (int n) {
- int mult;
- int fat = 1;
- for (mult = 1; mult <= n; mult++) {
- fat *= mult;
- }
- return fat;
- }
- //strlength da vida
- int tamStr(char word[]) {
- int tam = 0;
- while (word[tam] != '\0') {
- tam++;
- }
- return tam;
- }
- //calcula quadrado
- int sqrPwr (int n) {
- return n*n;
- }
- //macro -- calcula quadrado
- #define sqrPwr2(n) n*n
- //calcula quadrado como inline
- inline int sqrPwr3 (int n) {
- return n*n;
- }
- int main (void) {
- // int vet[size];
- int numbah, numbah2;
- time_t ti, tf;
- int mac, fun, inl;
- long int i;
- /*
- //atencao, estou alocando "minha string" para um PONTEIRO!
- char *str = "minha string";
- readVet(vet, size);
- printf("\nA soma do vet que voce entrou vale %d.", sumVet(vet, size));
- printf("\n\nA vet atual esta assim:\n");
- impressao(vet, size);
- printf("\nOrdenando agora...\n");
- bSort(vet, size);
- impressao(vet, size);
- printf("\n\nEntre um numero > ");
- scanf("%d", &numbah);
- printf("O fatorial de %d eh %d.", numbah, fat(numbah));
- // printf("\n\nEntre uma string > ");
- // fflush(stdin);
- // scanf("%[^\n]", str);
- printf("\n\nO tamanho de '%s' vale %d.", str, tamStr(str));
- */
- printf("\n\nEntre um numero > ");
- scanf("%d", &numbah2);
- //funcao
- ti = time(NULL);
- for (i = 0; i < 1000000000L; i++) {
- fun = sqrPwr(numbah2);
- }
- tf = time(NULL);
- printf("O quadrado de %d vale %d.\nSeu runtime por funca foi de %d.", numbah2, fun, tf-ti);
- //macro
- ti = time(NULL);
- for (i = 0; i < 1000000000L; i++) {
- mac = sqrPwr2(numbah2);
- }
- tf = time(NULL);
- printf("\n\nO quadrado de %d vale %d.\nSeu runtime por macro foi de %d.", numbah2, mac, tf-ti);
- //inline
- ti = time(NULL);
- for (i = 0; i < 1000000000L; i++) {
- inl = sqrPwr3(numbah2);
- }
- tf = time(NULL);
- printf("\n\nO quadrado de %d vale %d.\nSeu runtime por inlin foi de %d.", numbah2, inl, tf-ti);
- printf("\n\n");
- system("pause");
- return 0;
- }
Add Comment
Please, Sign In to add comment