forked from parallel101/course
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
338 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
target("exec") | ||
add_files("01.cpp") |
Binary file not shown.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
target("a") | ||
add_files("*.cpp") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters