C: Unterschied zwischen den Versionen

Aus Info-Theke
Zur Navigation springen Zur Suche springen
 
(2 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 2: Zeile 2:


= Links =
= Links =
* [[C-Beispiel-big.c]]
* [[C]]
* [[C-Beispiel-big.c]] Platte schnell füllen
* [[C-hamamips.c]] CPU-Benchmark
== Tutorials ==
* [https://www.learn-c.org/de/ learn-c.org (deutsch)]
* [http://www.c-programmieren.com/C-Lernen.html C-Lernen (deutsch), sehr ausführlich]


== CPU-Benchmark hamamips.c ==
= Umgebung =
<pre>#include "stdio.h"
* [https://www.heise.de/download/product/virtual-c-ide-94038 virtual-c: Live-Coding IDE mit Debugger]
#include "stdlib.h"
* [https://bloodshed-dev-c.de.softonic.com/ bloodshed-dev-c: Einfache IDE mittels MINGW]
#include "time.h"
* [http://www.codeblocks.org/ codeblocks: Editor mit MINGW]
#include "math.h"
 
typedef unsigned long seed_t;
= Ideen =
#define DEFINE_VARS(seed, x, y, z, c) seed_t seed = 0x20111958UL; \
* Random-Generator
seed_t x = 0x33221122UL; \
* Verschlüsselung
seed_t y = 0x73829382UL; \
* Tankprotokoll
seed_t z = 0xabcdef07UL; \
seed_t c = 0
#define next(seed, x, y, z, c) \
do { \
x = x * 0x20111957 + 0x11121989; \
y ^= y << 13; \
y ^= y >> 17; \
y ^= y << 5; \
seed_t t = 698769069UL * z + c; \
c = t << 31; \
z = t; \
seed = x + y + z; \
} while (0)
seed_t calcRandom(int millions){
DEFINE_VARS(seed, x, y, z, c);
seed_t rc = 0;
while (millions-- > 0){
int ix2 = 1000000;
while(ix2-- > 0){
next(seed, x, y, z, c);
rc += seed;
}
}
return rc;
}
int memSpeed(int megaBytes, int rounds){
        clock_t duration, duration2, start = clock();
        int ix, ix2, ix3, ix1, round;
        double** buffers = (double**) malloc(megaBytes * 1024 * 1024);
        int bufferEntries = 1024*1024/sizeof(double);
        for (ix = 0; ix < megaBytes; ix++){
                buffers[ix] = (double*) malloc(sizeof(double)*bufferEntries);
                for (ix2 = 0; ix2 < bufferEntries; ix2++)
                        buffers[ix][ix2] = ix2*ix;
        }
        duration2 = clock() - start;
        for (round = 0; round < rounds; round++)
                for (ix2 = bufferEntries - 1, ix3 = 0; ix2 >= 0; ix2--, ix3++)
                        for (ix = megaBytes - 1, ix1 = 0; ix >= 0; ix--, ix1++)
                                if (buffers[ix][ix2] != buffers[ix1][ix3] + buffers[ix1][ix2])
                                        buffers[ix][ix2] += buffers[ix][ix3] - buffers[ix1][ix3] + buffers[ix1][ix2];
        duration = clock() - start - duration2;
        printf("%.3f MemMips %.3f / %.4f sec %d  MiBytes %d rounds\n",
                pow(megaBytes, 1.3) * (double) CLOCKS_PER_SEC * rounds / duration / 16,
                duration / (double) CLOCKS_PER_SEC , duration2 / (double) CLOCKS_PER_SEC,
                megaBytes, rounds);
}
int main(int argc, char** argv){
        clock_t duration, start = clock();
        int millions = 100, megaBytes = 50, rounds = 1000  * millions / 400 / megaBytes;
        if (rounds < 1)
                rounds = 1;
        seed_t value;
        if (argc > 1 && atol(argv[1]) != 0)
                millions = atol(argv[1]);
        if (argc > 2 && atol(argv[2]) != 0)
                megaBytes = atol(argv[2]);
        value = calcRandom(millions);
        duration = clock() - start;
        printf ("%.3f HamaMips %.3f sec value: %04x-%04x\n",
                millions * (double) CLOCKS_PER_SEC / duration,
                duration / (double) CLOCKS_PER_SEC,
                (value >> 16) & 0xffff, value & 0xffff);
        memSpeed(megaBytes, rounds);
}
</pre>
gcc -O3 -lm hamamips.c && ./a.out

Aktuelle Version vom 14. Juni 2020, 07:01 Uhr