⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 memfile.cpp

📁 非常好用的五子棋游戏源码
💻 CPP
字号:
// Created:11-14-98
// By Jeff Connelly

// Memory vs. File I/O test

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "comprlib.h"
#include "allegro.h"        // Allegro required to compile this for timer

// Note: If 'i_love_bill' is set (by default, it is set if Windows is
//       detected), the timer is not as accurate.  Try running it in DOS,
//       or set 'i_love_bill' and ignore the dialog telling that it may not
//       run will unless in DOS.

// On my 80486DX PC, file I/O was about 55 milliseconds, and memory I/O
// about 50 milliseconds.  If you increase TEST_REP, it will take longer
// but may be more accurate.

// Modifiable defines
#define TEST_STR         "test "                         // String to repeat
#define TEST_REP         1000                            // this many times
#define TEST_LEN         5                               // Len of TEST_STR

// Globals
volatile int counter = 0;               // Counter variable
char* teststr;                          // String to compress

void on_timer(...)
{
    ++counter;
}
END_OF_FUNCTION(on_timer);

// This function performs the actual test
void do_test(int& file_result, int& mem_result)
{
    int i;

    // First we do a file I/O test
    ComprLibFileIO::Set();

    // Write 'teststr' to the input file.  Note that here we use
    // ComprLibFileIO::source_file as a temporary FILE*.
    ComprLibFileIO::source_file = fopen("MEMFILE.INP", "wb");
    fwrite (teststr, 1, strlen(teststr), ComprLibFileIO::source_file);
    fclose(ComprLibFileIO::source_file);
    ComprLibFileIO::source_file = fopen("MEMFILE.INP", "rb");
                                     
    // Open the destination file and validate
    ComprLibFileIO::dest_file = fopen("MEMFILE.OUT", "wb");
    if (!ComprLibFileIO::dest_file ||
        !ComprLibFileIO::source_file)
    {
        printf ("Cannot open file(s)\n");
        exit (2);
    }

    if (install_int(on_timer, 1) != 0)
    {
        printf ("Failed to install Allegro timer\n");
        exit (1);
    }

    lzss_encode();          // Encode it

    // Clean up
    fclose(ComprLibFileIO::dest_file);
    fclose(ComprLibFileIO::source_file);
    remove_int(on_timer);

    file_result = counter;

    // Now we do the memory test
    ComprLibMemIO::Set();
    counter = 0;
    ComprLibMemIO::source_len = (TEST_REP * TEST_LEN) + 1;
    ComprLibMemIO::source_ptr = teststr;
    ComprLibMemIO::dest_ptr = (char*)malloc(1);
    ComprLibMemIO::dest_len = 1;
    install_int(on_timer, 1);

    lzss_encode();      // Encode it

    // Clean-up
    mem_result = counter;
    remove_int(on_timer);
    free(ComprLibMemIO::dest_ptr);
}

// Initializes the test
void initialize()
{
    int i;

    // 'teststr' will contain 'TEST_STR' repeated 'TEST_REP' times
    teststr = (char*)malloc((TEST_LEN * TEST_REP) + 1);
    memset ((void*)teststr, 0, (TEST_LEN * TEST_REP) + 1);
    for (i = 0; i < TEST_REP; ++i)
    {
        strcat (teststr, TEST_STR);
    }

    // Initialize the function handler and initialize allegro
    LOCK_VARIABLE(counter);
    LOCK_FUNCTION(on_timer);
    allegro_init();
    install_timer();
}

// Clean-up
void deinitialize()
{
    // Clean-up of whole program
    free(teststr);
}    

int main(int argc, char* argv[])
{
    int file, mem, a, b;

    file = mem = a = b = 0;

    printf ("Initializing...\n");
    initialize();
    printf ("Press any key to stop.\n");

    // The results can off a little, so we loop until a keypress and an
    // average them together
    while (!kbhit())
    {
        do_test(a, b);
        // Note: >> 1 is equalvilant to / 2, but faster
        file = (a + file) >> 1;     
        mem  = (b + mem ) >> 1;
        printf ("File: %d, Memory %d\n", a, b);
    }
    deinitialize();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -