- 1
"Performance is easy. All you need to know is everything" (c) S. Kuksenko (@kuksenk0)
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
+1
"Performance is easy. All you need to know is everything" (c) S. Kuksenko (@kuksenk0)
Умеете профилировать программы?
Знаете, что такое flame diagram? Умеете посмотреть стектрейс от входа в тред до какого-нить спинлока в ядре? Смотрите асм своего компилятора или джита? Знаете, какая инструкция сколько занимает?
Умеете по vmstat/iostat или xperf/perf counters увидать проблему и соотнести ее с программой? Используете dtrace голый или с instruments? yourkit? vTune? valgrind? bpf?
Или такие же дебилы, как я?
0
#include <stdlib.h>
#define printf() print()
int main(void)
{
int a = 0;
a = a++ + ++a;
print("I'm using macro that redefine printf() function (%d).", a);
return 0;
}
Ыыыыыыыы
суть говнокода же чтоб он собрал максимум красных цифр?
это не мой код честно!!!
−102
Всё. Петухи закончились. Всем спасибо, все свободны.
+1
template < typename T >
T shit (void)
{
return 0;
}
int main()
{
int crap = shit();
// Почему дедукция аргумента шаблона в данном случае не работает?
return crap;
}
//-------------------------------------
int shit (void)
{
return 0;
}
// Почему functions that differ only in their return type cannot be overloaded
double shit (void)
{
return 0;
}
int main()
{
int crap = shit();
return crap;
}
Почему плюсы такое говно?
0
#include <algorithm>
#include <exception>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <random>
#include <chrono>
#define __CL_ENABLE_EXCEPTIONS
#include "cl.hpp"
using namespace std::string_literals;
int main(int argc, char * argv[]) {
size_t data_len = (1024 * 1024 * strtoul(argv[1], nullptr, 10)) / sizeof(uint32_t),
out_len = strtoul(argv[2], nullptr, 10),
iter = strtoul(argv[3], nullptr, 10),
block_size = strtoul(argv[4], nullptr, 10);
std::string src = R"(
typedef unsigned int uint32_t;
__kernel void bench(global const uint32_t * data, global uint32_t * out) {
uint32_t res = 0, id = get_global_id(0), next = id;
for(uint32_t i = 0; i < )"s + std::to_string(iter) + R"(; ++i) {
for(uint32_t j = 0; j < )" + std::to_string(block_size / sizeof(uint32_t)) + R"(; ++j)
res ^= data[next + j];
next = data[next];
}
out[id] = res;
}
)"s;
cl::Program::Sources sources = {{src.data(), src.size()}};
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
std::vector<uint32_t> data(data_len);
std::vector<uint32_t> out(out_len);
std::generate(std::begin(data), std::end(data), [=,gen = std::mt19937{}]() mutable {
return gen() % (data_len - (block_size / sizeof(uint32_t)));
});
for(auto & platform : platforms) {
std::cout << "Using platform: " << platform.getInfo<CL_PLATFORM_NAME>() << "\n";
std::vector<cl::Device> devices;
platform.getDevices(CL_DEVICE_TYPE_GPU, &devices);
for(auto & device : devices) {
try {
std::cout << "Using device: " << device.getInfo<CL_DEVICE_NAME>() << "\n";
cl::Context ctx({device});
cl::Program program(ctx, sources);
program.build({device});
cl::Buffer data_buffer(ctx, CL_MEM_READ_WRITE, data.size() * sizeof(uint32_t));
cl::Buffer out_buffer(ctx, CL_MEM_READ_WRITE, out.size() * sizeof(uint32_t));
cl::CommandQueue queue(ctx, device);
queue.enqueueWriteBuffer(data_buffer, CL_TRUE, 0, data.size() * sizeof(uint32_t), data.data());
cl::make_kernel<cl::Buffer &, cl::Buffer &> bench(program, "bench");
cl::EnqueueArgs eargs(queue,cl::NullRange,cl::NDRange(out.size()),cl::NullRange);
auto start = std::chrono::high_resolution_clock::now();
bench(eargs, data_buffer, out_buffer).wait();
auto time = std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::high_resolution_clock::now() - start).count();
size_t ops = out_len * iter;
size_t total_tp = ops * block_size;
double miops = (ops / time) / (1000 * 1000);
double tpgbps = (total_tp / time) / (1024 * 1024 * 1024);
fprintf(stderr, "Result: %.2fMIOPS, %.2fGB/s, %.2fsec\n", miops, tpgbps, time);
queue.enqueueReadBuffer(out_buffer, CL_TRUE, 0, out.size() * sizeof(uint32_t), out.data());
} catch(cl::Error e) {
std::cout << e.what() << " : " << e.err() << std::endl;
std::terminate();
}
}
}
}
Код Царя
https://www.linux.org.ru/forum/development/13489159
https://github.com/superhackkiller1997/gpu_mem_benchmark/blob/master/main.cpp
+2000
unsigned int get_spoofed() {
char spa[21];
int a, b, c, d;
srand(time(0));
random_ct = rand();
random_num = ((random_ct % 254) + 1);
a = random_num;
random_ct = rand();
random_num = ((random_ct % 254) + 1);
b = random_num;
random_ct = rand();
random_num = ((random_ct % 254) + 1);
c = random_num;
random_ct = rand();
random_num = ((random_ct % 254) + 1);
d = random_num;
snprintf(spa, sizeof(spa), "%d.%d.%d.%d", a, b, c, d);
return ((unsigned int)host2ip(spa));
}
Ддосбот для роутеров https://github.com/eurialo/lightaidra/blob/master/source/utils.c
+2
#define CREATE_EVENT_LISTENER(_elname, arg1_type, arg1_name) \
class _elname : public EventListener \
{ \
private: \
class IContainer \
{ \
public: \
virtual void Call(arg1_type arg1_name) = 0; \
virtual ~IContainer() {} \
}; \
\
class FunctionContainer : public IContainer \
{ \
private: \
typedef void(*__CallbackPtr)(arg1_type); \
public: \
FunctionContainer(__CallbackPtr fn) \
{ \
this->fn = fn; \
} \
\
virtual void Call(arg1_type arg1_name) \
{ \
fn(arg1_name); \
} \
\
private: \
__CallbackPtr fn; \
}; \
\
template<class T, class Q> \
class MethodContainer : public IContainer \
{ \
public: \
MethodContainer(T method, Q _this) \
{ \
this->method = method; \
this->_this = _this; \
} \
\
virtual void Call(arg1_type arg1_name ) \
{ \
(_this->*method)(arg1_name); \
} \
\
private: \
T method; \
Q _this; \
}; \
public: \
typedef void(*__FN_CALLBACK)(arg1_type); \
\
_elname(__FN_CALLBACK fn) \
{ \
this->container = new FunctionContainer(fn); \
} \
\
template <class T, class Q> \
_elname(T method, Q _this) \
{ \
this->container = new MethodContainer<T, Q>(method, _this); \
} \
\
void Call(arg1_type arg1_name) \
{ \
container->Call(arg1_name); \
} \
\
virtual ~_elname() \
{ \
delete this->container; \
} \
private: \
IContainer* container; \
}; \
#define CREATE_EVENT(_ename, _elname, arg1_type, arg1_name) \
class _ename : public Event \
{ \
public: \
void AddListener(_elname* listener) \
{ \
Event::AddListener(listener); \
} \
\
void Handle(arg1_type arg1_name) \
{ \
for (size_t i = 0; i < this->listeners.size(); i++) \
{ \
((_elname*)listeners[i])->Call(arg1_name); \
} \
} \
\
void RemoveListener(_elname* listener) \
{ \
Event::RemoveListener(listener); \
} \
\
}; \
Я когда то это написал. Думал, это хорошая идея...
Полный файл: https://github.com/arhyme/CPP_EVENTS/blob/master/Event.h
+161
function UpdateTime() {
var CurrentTime = new Date();
var InputTime = document.getElementById('MyTime');
var InputDate = document.getElementById('MyDate');
sec=sec+1;
if(sec >=58)
{
if(min>59)
{
hour=hour+1;
min=0;
} else
{ min=min+1;
};
sec=0;
} else {
};
h = hour;
if ( h < 10 ) h = "0" + h;
m = min;
if ( m < 10 ) m = "0" + m;
s = sec;
if ( s < 10 ) s = "0" + s;
outString = h + ":" + m + ":" + s;
InputTime.innerHTML = outString;
outString = d + " ";
outString += month[mo] + " ";
outString += y;
InputDate.innerHTML = outString;
setTimeout("UpdateTime()",1000);
}
Надо было человеку время написать на сайте, текущее...
И ОНО сделало ЭТО.
И этот код встречается на каждой странице проекта. Постоянно 1 и тот же. А верстку лучше даже не смотреть....
Уже около часа не знаю с какой стороны подобраться к этому поделию(в основном к верстке)...
+19
wstring& delphi::IntToStr(int integer, wstring& str)
{
if (0 == integer)
return str = L"0";
str.clear();
wstring sign(L"");
if (integer < 0)
{
sign = L"-";
integer = -integer;
}
else
sign = L"";
while (integer >= 1)
{
str.push_back( (integer % 10) + 48 );
integer /= 10;
}
str += sign;
std::reverse(str.begin(), str.end());
return str;
}
+133
#ifndef ORDER32_H
#define ORDER32_H
#include <limits.h>
#include <stdint.h>
#if CHAR_BIT != 8
#error "unsupported char size"
#endif
enum
{
O32_LITTLE_ENDIAN = 0x03020100ul,
O32_BIG_ENDIAN = 0x00010203ul,
O32_PDP_ENDIAN = 0x01000302ul
};
static const union { unsigned char bytes[4]; uint32_t value; } o32_host_order =
{ { 0, 1, 2, 3 } };
#define O32_HOST_ORDER (o32_host_order.value)
#endif
Говнокод из http://stackoverflow.com/questions/2100331/c-macro-definition-to-determine-big-endian-or-little-endian-machine
Мало того, что писать в один тип из юниона и потом читать из другого это UB, так еще компилятор (в случае GCC) из
int main(void)
{return O32_HOST_ORDER == O32_LITTLE_ENDIAN;}
main:
xor eax, eax
cmp DWORD PTR o32_host_order[rip], 50462976
sete al
ret
o32_host_order:
.byte 0
.byte 1
.byte 2
.byte 3