Skip to content

[pull] master from parallel101:master #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified 17/slides.pptx
Binary file not shown.
24 changes: 24 additions & 0 deletions stlseries/stl_map/.vim_localrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
" vim: ft=vim

func! MarkPG()
let pgidx = 1
let [bufnum, old_lnum, old_col, old_off] = getpos('.')
norm 1G0
let [lnum, col] = searchpos('^---\n\n', 'cnW')
while lnum != 0
exec 'norm! '.lnum.'G0'
let [mark_lnum, mark_col] = searchpos('^<!-- PG\d\+ -->$', 'nW')
let [lnum, col] = searchpos('^---\n\n', 'nW')
if mark_lnum == 0 || (lnum != 0 && mark_lnum > lnum)
exec 'norm! o'
exec 'norm! o<!-- PG'.pgidx.' -->'
if lnum != 0
let lnum = lnum + 2
endif
else
exec 'norm! '.mark_lnum.'G0c$<!-- PG'.pgidx.' -->'
endif
let pgidx = pgidx + 1
endwhile
call cursor(old_lnum, old_col, old_off)
endfunc
124 changes: 124 additions & 0 deletions stlseries/stl_map/experiment/ScopeProfiler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#pragma once

#include <chrono>
#include <iostream>
#include <vector>
#include <iomanip>
#include <string_view>
#include <map>
#include <set>

class ScopeProfiler {
public:
using ClockType = std::chrono::high_resolution_clock;

struct Record {
const char *tag;
int us;
};

private:
inline thread_local static std::vector<Record> records;

ClockType::time_point beg;
ClockType::time_point end;
const char *tag;

inline ScopeProfiler(const char *tag, ClockType::time_point beg);
inline void onDestroy(ClockType::time_point end);

public:
ScopeProfiler(const char *tag_) : ScopeProfiler(tag_, ClockType::now()) {}
~ScopeProfiler() { onDestroy(ClockType::now()); }

static std::vector<Record> const &getRecords() { return records; }
inline static void printLog(std::ostream &out = std::cout);
};

ScopeProfiler::ScopeProfiler(const char *tag_, ScopeProfiler::ClockType::time_point beg_)
: beg(beg_), tag(tag_)
{
}

void ScopeProfiler::onDestroy(ScopeProfiler::ClockType::time_point end) {
auto diff = end - beg;
int us = std::chrono::duration_cast<std::chrono::microseconds>(diff).count();
records.push_back({tag, us});
}

void ScopeProfiler::printLog(std::ostream &out) {
if (records.size() == 0) {
return;
}

struct Statistic {
int max_us = 0;
int min_us = 0;
int total_us = 0;
int count_rec = 0;
const char *tag = nullptr;
};
std::map<std::string_view, Statistic> stats;
for (auto const &[tag, us]: records) {
auto &stat = stats[tag];
stat.total_us += us;
stat.max_us = std::max(stat.max_us, us);
stat.min_us = !stat.count_rec ? us : std::min(stat.min_us, us);
stat.count_rec++;
stat.tag = tag;
}

struct StatisticCompare {
using value_type = std::pair<std::string_view, Statistic>;
bool operator()(value_type const &lhs, value_type const &rhs) const {
return lhs.second.total_us > rhs.second.total_us;
}
};

std::multiset<std::pair<std::string_view, Statistic>, StatisticCompare> sortstats(stats.begin(), stats.end());

auto dump = [&out] (int val, int w) {
auto tpwv = 1;
for (int i = 0; i < w - 1; i++) tpwv *= 10;
if (val > tpwv) {
if (val / 1000 > tpwv / 10) {
out << std::setw(w - 1) << val / 1000000 << 'M';
} else {
out << std::setw(w - 1) << val / 1000 << 'k';
}
} else {
out << std::setw(w) << val;
}
};

out << " avg | min | max | total | cnt | tag\n";
for (auto const &[tag, stat]: sortstats) {
dump(stat.total_us / stat.count_rec, 9); out << '|';
dump(stat.min_us, 9); out << '|';
dump(stat.max_us, 9); out << '|';
dump(stat.total_us, 9); out << '|';
dump(stat.count_rec, 5); out << '|';
out << ' ' << tag << '\n';
}
}

#if defined(__GUNC__) || defined(__clang__)
#define DefScopeProfiler ScopeProfiler _scopeProfiler(__PRETTY_FUNCTION__);
#elif defined(_MSC_VER)
#define DefScopeProfiler ScopeProfiler _scopeProfiler(__FUNCSIG__);
#else
#define DefScopeProfiler ScopeProfiler _scopeProfiler(__func__);
#endif

template <class T>
static
#if defined(__GUNC__) || defined(__clang__)
__attribute__((noinline))
#elif defined(_MSC_VER)
__declspec(noinline)
#endif
void doNotOptimize(T volatile const &t) {}

static void printScopeProfiler(std::ostream &out = std::cout) {
ScopeProfiler::printLog(out);
}
5 changes: 0 additions & 5 deletions stlseries/stl_map/experiment/a.cpp

This file was deleted.

24 changes: 9 additions & 15 deletions stlseries/stl_map/experiment/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,17 @@
#include "print.h"
#include "cppdemangle.h"
#include "map_get.h"
#include "ScopeProfiler.h"
using namespace std;

int main() {
map<string, string> msg = {
{"hello", "world"},
{"fuck", "rust"},
};
print(msg);
if (msg.count("fuck")) {
print("存在fuck,其值为", msg.at("fuck"));
} else {
print("找不到fuck");
}
if (msg.count("suck")) {
print("存在suck,其值为", msg.at("suck"));
} else {
print("找不到suck");
}
struct MyData {
int value;
explicit MyData(int value_) : value(value_) {}
};
map<string, unique_ptr<MyData>> m;
m.insert({"answer", make_unique<MyData>(42)});
m.insert({"fuck", make_unique<MyData>(985)});
print(m.at("answer")->value); // 42
return 0;
}
31 changes: 31 additions & 0 deletions stlseries/stl_map/experiment/testemplace.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <bits/stdc++.h>
#include "print.h"
#include "cppdemangle.h"
#include "map_get.h"
#include "ScopeProfiler.h"
using namespace std;

struct MyClass {
int arr[4096];
};

template <class K, class V>
static void test_insert(map<K, V> &tab) {
DefScopeProfiler;
for (int i = 0; i < 1000; i++) {
tab.insert({i, {}});
}
}

template <class K, class V>
static void test_try_emplace(map<K, V> &tab) {
DefScopeProfiler;
for (int i = 0; i < 1000; i++) {
tab.try_emplace(i);
}
}

int main() {
map<
return 0;
}
39 changes: 39 additions & 0 deletions stlseries/stl_map/experiment/testraii.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <bits/stdc++.h>
#include "print.h"
#include "cppdemangle.h"
#include "map_get.h"
#include "ScopeProfiler.h"
using namespace std;

struct RAII {
int i;

explicit RAII(int i_) : i(i_) {
printf("%d号资源初始化\n", i);
}

RAII(RAII &&) noexcept {
printf("%d号资源移动\n", i);
}

RAII &operator=(RAII &&) noexcept {
printf("%d号资源移动赋值\n", i);
return *this;
}

~RAII() {
printf("%d号资源释放\n", i);
}
};

int main() {
{
map<string, RAII> m;
m.try_emplace("资源1号", 1);
m.try_emplace("资源2号", 2);
m.erase("资源1号");
m.try_emplace("资源3号", 3);
}
printf("此时所有资源都应该已经释放\n");
return 0;
}
Binary file added stlseries/stl_map/images/logicmap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added stlseries/stl_map/images/physmap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added stlseries/stl_map/images/setvsmap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added stlseries/stl_map/images/sortedset.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading