Skip to content

Instantly share code, notes, and snippets.

@Southclaws
Last active January 3, 2016 15:19
Show Gist options
  • Save Southclaws/8482426 to your computer and use it in GitHub Desktop.
Save Southclaws/8482426 to your computer and use it in GitHub Desktop.
File function tests
#define FILTERSCRIPT
#include <a_samp>
#include <sscanf>
#include <y_speedtest>
#define BINARY_FILE "test.dat"
#define ENCODE_FILE "test.txt"
#define TESTS 1000
public OnFilterScriptInit()
{
// Create the files first.
fclose(fopen(BINARY_FILE, io_write));
fclose(fopen(ENCODE_FILE, io_write));
// Run the tests
TEST_01_BIN();
TEST_01_ENC();
print("\n\n");
TEST_02_BIN();
TEST_02_ENC();
print("\n\n");
TEST_03_BIN();
TEST_03_ENC();
}
/*==============================================================================
TEST #1: Basic block of 32768 bits (4096 bytes).
Description:
The binary test writes 1024 32 bit cells to the file whereas the encoded
test writes 4096 8bit 'A' characters to the file. The file sizes are the
same and this test is purely to see which one can write the same amount
of data faster.
==============================================================================*/
new
t1_arr[1024] = {32, ...},
t1_str[4096] = {65, ...};
TEST_01_BIN()
{
new
File:file;
t1_str[3999] = EOS;
// WRITE
RUN_TIMING("TEST 01 :: BINARY WRITE", TESTS)
{
file = fopen(BINARY_FILE, io_write);
fblockwrite(file, t1_arr);
fclose(file);
}
// READ
RUN_TIMING("TEST 01 :: BINARY READ", TESTS)
{
file = fopen(BINARY_FILE, io_read);
fblockread(file, t1_arr);
fclose(file);
}
}
TEST_01_ENC()
{
new
File:file;
// WRITE
RUN_TIMING("TEST 01 :: ENCODED WRITE", TESTS)
{
file = fopen(ENCODE_FILE, io_write);
fwrite(file, t1_str);
fclose(file);
}
// READ
RUN_TIMING("TEST 01 :: ENCODED READ", TESTS)
{
file = fopen(ENCODE_FILE, io_read);
fread(file, t1_str);
fclose(file);
}
}
/*==============================================================================
TEST #2: Writing and reading 8 integers.
A more real situation where you want to write a set of integers to a
file for later retrieval. The binary method is simpler and only requires
an array list of raw numbers which is read in the same way. However, the
encoded method requires a string of correctly formatted and human
readable numbers with delimiters. This test is to simulate a real
situation so delimiters are added as well as a method to extract data.
==============================================================================*/
#define SIZE 256 // Lets write 128 numbers to a file.
new
t2_arr[SIZE], // No matter how big the number, it uses 32 bits.
t2_str[SIZE * 4]; // More space for 2/3 digit numbers as well as delimiters.
TEST_02_BIN()
{
new
File:file,
tmp[5]; // "00, "
// Build the lists
// Ternary thing is to determine digit length for string.
for(new i; i < SIZE; i++)
{
// Lazy code
t2_arr[i] = i;
format(tmp, 5, "%03d|", i);
strcat(t2_str, tmp);
}
// WRITE
RUN_TIMING("TEST 02 :: BINARY WRITE", TESTS)
{
file = fopen(BINARY_FILE, io_write);
fblockwrite(file, t2_arr);
fclose(file);
}
// READ
RUN_TIMING("TEST 02 :: BINARY READ", TESTS)
{
file = fopen(BINARY_FILE, io_read);
fblockread(file, t2_arr);
fclose(file);
}
}
TEST_02_ENC()
{
new
File:file,
tmp[SIZE];
// WRITE
RUN_TIMING("TEST 02 :: ENCODED WRITE", TESTS)
{
file = fopen(ENCODE_FILE, io_write);
fwrite(file, t2_str);
fclose(file);
}
// READ
RUN_TIMING("TEST 02 :: ENCODED READ", TESTS)
{
file = fopen(ENCODE_FILE, io_read);
fread(file, t2_str);
// With a string, we have to extract the data still because it's still
// in string form. This is included in the timing as it's part of the
// entire process.
sscanf(t2_str, "p<|>a<d>["#SIZE"]", tmp);
fclose(file);
}
}
/*==============================================================================
TEST #3:
==============================================================================*/
TEST_03_BIN()
{
}
TEST_03_ENC()
{
}
Timing "TEST 01 :: BINARY WRITE"...
Mean = 283.00us
Mode = 257.00us
Median = 266.00us
Range = 157.00us
Timing "TEST 01 :: BINARY READ"...
Mean = 73.00us
Mode = 73.00us
Median = 73.00us
Range = 4.00us
Timing "TEST 01 :: ENCODED WRITE"...
Mean = 409.00us
Mode = 360.00us
Median = 378.00us
Range = 286.00us
Timing "TEST 01 :: ENCODED READ"...
Mean = 163.00us
Mode = 162.00us
Median = 162.00us
Range = 8.00us
Timing "TEST 02 :: BINARY WRITE"...
Mean = 268.00us
Mode = 228.00us
Median = 245.00us
Range = 262.00us
Timing "TEST 02 :: BINARY READ"...
Mean = 39.00us
Mode = 40.00us
Median = 40.00us
Range = 1.00us
Timing "TEST 02 :: ENCODED WRITE"...
Mean = 313.00us
Mode = 261.00us
Median = 272.00us
Range = 197.00us
Timing "TEST 02 :: ENCODED READ"...
Mean = 69.00us
Mode = 67.00us
Median = 67.00us
Range = 11.00us
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment