Skip to content

Commit

Permalink
old
Browse files Browse the repository at this point in the history
  • Loading branch information
archibate committed Aug 16, 2022
1 parent 56e79dc commit 7abcc54
Show file tree
Hide file tree
Showing 21 changed files with 338 additions and 2 deletions.
16 changes: 16 additions & 0 deletions 16/00.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <iostream>
#include <string>
#include <map>

using namespace std;

int main() {
map<string, int> items = {
{"hello", 1},
{"world", 2},
};

items["time"] = 4;
int time = items.at("time");
cout << time << endl;
}
8 changes: 8 additions & 0 deletions 16/00.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
items = dict([
("hello", 1),
("world", 2),
])

items["time"] = 4
time = items["time"]
print(time)
15 changes: 15 additions & 0 deletions 16/00a.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <iostream>
#include <string>
#include <map>

using namespace std;

int main() {
map<string, int> items = {
{"hello", 1},
{"world", 2},
};

int time = items["hello"];
cout << time << endl;
}
15 changes: 15 additions & 0 deletions 16/00b.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <iostream>
#include <string>
#include <map>

using namespace std;

int main() {
map<string, int> items = {
{"hello", 1},
{"world", 2},
};

int time = items["hell"];
cout << time << endl;
}
13 changes: 13 additions & 0 deletions 16/01.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <iostream>
#include <map>
#include "printer.h"

using namespace std;

int main() {
map<string, int> m;
m["hello"] = 1;
m["world"] = 2;
cout << m << endl;
return 0;
}
13 changes: 13 additions & 0 deletions 16/02.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <iostream>
#include <map>
#include "printer.h"

using namespace std;

int main() {
map<string, int> m;
m.at("hello") = 1;
m.at("world") = 2;
cout << m << endl;
return 0;
}
39 changes: 39 additions & 0 deletions 16/printer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <iostream>
#include <utility>
#include <type_traits>

namespace std {

template <class T, class = const char *>
struct __printer_test_c_str {
using type = void;
};

template <class T>
struct __printer_test_c_str<T, decltype(std::declval<T>().c_str())> {};

template <class T, int = 0, int = 0, int = 0,
class = decltype(std::declval<std::ostream &>() << *++std::declval<T>().begin()),
class = decltype(std::declval<T>().begin() != std::declval<T>().end()),
class = typename __printer_test_c_str<T>::type>
std::ostream &operator<<(std::ostream &os, T const &v) {
os << '{';
auto it = v.begin();
if (it != v.end()) {
os << *it;
for (++it; it != v.end(); ++it)
os << ',' << *it;
}
os << '}';
return os;
}

template <class T1, class T2>
std::ostream &operator<<(std::ostream &os, std::pair<T1, T2> const &v) {
os << '{' << v.first << ',' << v.second << '}';
return os;
}

}
Binary file modified 16/slides.pptx
Binary file not shown.
2 changes: 2 additions & 0 deletions 16/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target("exec")
add_files("01.cpp")
Binary file added Presentation1.pptx
Binary file not shown.
Empty file added specifelse/.tasks
Empty file.
35 changes: 35 additions & 0 deletions specifelse/0.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "ticktock.h"
#include "randint.h"
#include <vector>
#include <algorithm>

#if 0
__attribute__((noinline)) void uppercase(char *p, int n) {
for (int i = 0; i < n; i++) {
p[i] = ('a' <= p[i] && p[i] <= 'z') ? p[i] + 'A' - 'a' : p[i];
}
}
#else
__attribute__((noinline)) void uppercase(char *p, int n) {
for (int i = 0; i < n; i++) {
if ('a' <= p[i] && p[i] <= 'z')
p[i] = p[i] + 'A' - 'a';
}
}
#endif

int main() {
int n = (int)1e7;
std::vector<char> a(n);

for (int i = 0; i < n; i++) {
a[i] = randint<char>(0, 127);
}
std::sort(a.begin(), a.end());

TICK(test);
uppercase(a.data(), n);
TOCK(test);

return 0;
}
10 changes: 10 additions & 0 deletions specifelse/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.18)

if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
set(CMAKE_CXX_STANDARD 20)

project(main LANGUAGES CXX)

add_executable(main main.cpp)
35 changes: 35 additions & 0 deletions specifelse/old/1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "ticktock.h"
#include "randint.h"
#include <vector>
#include <algorithm>

__attribute__((noinline)) void uppercase(char *p, int n) {
for (int i = 0; i < n; i++) {
if ('a' <= p[i] && p[i] <= 'z')
p[i] = p[i] + 'A' - 'a';
}
}

int main() {
int n = (int)1e7;
std::vector<char> a(n);

for (int i = 0; i < n; i++) {
a[i] = randint<char>(0, 127);
}

TICK(random);
uppercase(a.data(), n);
TOCK(random);

for (int i = 0; i < n; i++) {
a[i] = randint<char>(0, 127);
}
std::sort(a.begin(), a.end());

TICK(sorted);
uppercase(a.data(), n);
TOCK(sorted);

return 0;
}
34 changes: 34 additions & 0 deletions specifelse/old/2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "ticktock.h"
#include "randint.h"
#include <vector>
#include <algorithm>

__attribute__((noinline)) void uppercase(char *p, int n) {
for (int i = 0; i < n; i++) {
p[i] = ('a' <= p[i] && p[i] <= 'z') ? (p[i] + 'A' - 'a') : p[i];
}
}

int main() {
int n = (int)1e7;
std::vector<char> a(n);

for (int i = 0; i < n; i++) {
a[i] = randint<char>(0, 127);
}

TICK(random);
uppercase(a.data(), n);
TOCK(random);

for (int i = 0; i < n; i++) {
a[i] = randint<char>(0, 127);
}
std::sort(a.begin(), a.end());

TICK(sorted);
uppercase(a.data(), n);
TOCK(sorted);

return 0;
}
34 changes: 34 additions & 0 deletions specifelse/old/3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "ticktock.h"
#include "randint.h"
#include <vector>
#include <algorithm>

__attribute__((noinline)) void uppercase(char *p, int n) {
for (int i = 0; i < n; i++) {
p[i] = ('a' <= p[i] && p[i] <= 'z') ? (p[i] + 'A' - 'a') : p[i];
}
}

int main() {
int n = (int)1e7;
std::vector<char> a(n);

for (int i = 0; i < n; i++) {
a[i] = randint<char>(0, 127);
}

TICK(random);
uppercase(a.data(), n);
TOCK(random);

for (int i = 0; i < n; i++) {
a[i] = randint<char>(0, 127);
}
std::sort(a.begin(), a.end());

TICK(sorted);
uppercase(a.data(), n);
TOCK(sorted);

return 0;
}
47 changes: 47 additions & 0 deletions specifelse/old/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "ticktock.h"
#include "randint.h"
#include <vector>
#include <algorithm>

__attribute__((noinline)) void uppercase_slow(char *p, int n) {
for (int i = 0; i < n; i++) {
if ('a' <= p[i] && p[i] <= 'z')
p[i] = p[i] + 'A' - 'a';
}
//random: 0.020724s
//sorted: 0.004538s
}

__attribute__((noinline)) void uppercase_fast(char *p, int n) {
for (int i = 0; i < n; i++) {
p[i] = ('a' <= p[i] && p[i] <= 'z') ? (p[i] + 'A' - 'a') : p[i];
}
//random: 0.000735s (28x faster)
//sorted: 0.000774s
}

#define uppercase uppercase_fast

int main() {
int n = (int)1e7;
std::vector<char> a(n);

for (int i = 0; i < n; i++) {
a[i] = randint<char>(0, 127);
}

TICK(random);
uppercase(a.data(), n);
TOCK(random);

for (int i = 0; i < n; i++) {
a[i] = randint<char>(0, 127);
}
std::sort(a.begin(), a.end());

TICK(sorted);
uppercase(a.data(), n);
TOCK(sorted);

return 0;
}
11 changes: 11 additions & 0 deletions specifelse/randint.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include <random>

template <class T>
static T randint(T minVal, T maxVal) {
static std::mt19937 gen(0);
std::uniform_int_distribution<char> uni(minVal, maxVal);
return uni(gen);
}

7 changes: 7 additions & 0 deletions specifelse/ticktock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include <chrono>
#include <cstdio>

#define TICK(x) auto bench_##x = std::chrono::steady_clock::now();
#define TOCK(x) std::printf("%s: %lfs\n", #x, std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::steady_clock::now() - bench_##x).count());
2 changes: 2 additions & 0 deletions specmacro/xmake.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target("a")
add_files("*.cpp")
4 changes: 2 additions & 2 deletions tools/qt-danmu.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ def __init__(self, parent=None):
self.update()

def update(self):
threading.Timer(1, self.update).start()
threading.Timer(5, self.update).start()
msgs = get_messages()
print('update got:', msgs)
# print('update got:', msgs)
self.slm.setStringList(msgs)


Expand Down

0 comments on commit 7abcc54

Please sign in to comment.