# Faster

Here we have a serial program`serial.c`:

{% code title="serial.c" %}

```c
#include <stdio.h>
#include <omp.h>
#include <time.h>
#include <sys/time.h>
void test() {
    int a = 0;
    int i;
    for(i=0; i<1000000000; i++) {
        a++;
    }
}

int main() {
    struct timeval start, end;
    gettimeofday(&start, NULL);
    int i;

    for(i=0; i<8; i++) {
        test();
    }
    gettimeofday(&end, NULL);
    printf("time = %f seconds\n", (double)((end.tv_sec * 1000000 + end.tv_usec)- (start.tv_sec * 1000000 + start.tv_usec))/1000000);
}
```

{% endcode %}

```c
# compile with gnu gcc
gcc -o serial serial.c -fopenmp
# compile with clang (on mac)
clang -Xpreprocessor -fopenmp serial.c -lomp -oserial
```

```
$ ./serial
time = 20.059342 seconds
```

*Notice that the output time may be different on your own computer, that's OK.*

Now we use 4 (the default setting of my machine) threads to see how fast we can get!

{% code title="parallel\_with\_openmp.c" %}

```c
#include <stdio.h>
#include <omp.h>
#include <time.h>
#include <sys/time.h>
void test() {
    int a = 0;
    int i;
    for(i=0; i<1000000000; i++) {
        a++;
    }
}

int main() {
    struct timeval start, end;
    gettimeofday(&start, NULL);
    int i;
    #pragma omp parallel for
    for(i=0; i<8; i++) {
        test();
    }
    gettimeofday(&end, NULL);
    printf("time = %f seconds\n", (double)((end.tv_sec * 1000000 + end.tv_usec)- (start.tv_sec * 1000000 + start.tv_usec))/1000000);
}
```

{% endcode %}

```c
# compile with gnu gcc
gcc -o parallel parallel_with_openmp.c -fopenmp
# compile with clang (on mac)
clang -Xpreprocessor -fopenmp parallel_with_openmp.c -lomp -oparallel
```

```
$ ./parallel
time = 5.337185 seconds
```

That's amazing! As I know when a biology scientist wants to calculate the structure of some proteins, it can even take him/her time-cost from months down to weeks!


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://eric-lo.gitbook.io/openmp/23-faster.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
