#include<bits/stdc++.h>#define int long long#define endl '\n'typedef long long ll;
using namespace std;
string s;
signed main(){
cin >> s;
int pos = s.find(":");
int a = atoll(s.substr(0,pos).c_str()),b = atoll(s.substr(pos + 1).c_str());
cout << a << " " << b;
}
csanf/printf:
C++
#include<bits/stdc++.h>#define int long long#define endl '\n'typedef long long ll;
using namespace std;
int a,b;
signed main(){
scanf("%d:%d",&a,&b);
printf("%d %d",a,b);
}
2.3.快读快写
众所周知,在 C++ 中有两个函数 getchar() 和 putchar(),它们分别是用来读入和输出一个字符,效率也十分的快。
Windows 是 Windows 11,版本 23H2(OS 内部版本 22635.2700)。编译选项是 -std=c++14 -static -Wall -Wextra -Wl,-stack=536870912 -O2。
Linux 用的是 Hydro 的评测机。
首先,这是用来生成输入的代码:
C++
#include<bits/stdc++.h>#define int long long#define endl '\n'typedef long long ll;
using namespace std;
const int N = 300;
int a[10] = {1,1,10,100,1000,10000};
signed main(){
mt19937_64 rand(time(0));
for(int i = 1;i <= 5;i++){
string file = "test" + to_string(i) + ".in";
freopen(file.c_str(),"w",stdout);
cout << N * a[i] << endl;
for(int j = 1;j <= N * a[i];j++)cout << rand() << " ";
fclose(stdout);
}
}
这段代码会生成 5 个文件,分别是 test1.in ~ test5.in。数据有梯度,第 i 个文件会有 3×10i+1 个数。
生成的 5 个文件总共的大小是 35,805,118 字节,大约是 34 MB。
cin/cout + 关闭同步流测试代码:
C++
#include<bits/stdc++.h>#define int long long#define endl '\n'typedef long long ll;
using namespace std;
const int N = 3e7 + 5;
int n,a[N];
clock_t st,en;
double sum;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
freopen("output.txt","w",stdout);
for(int i = 1;i <= 5;i++){
string file = "test" + to_string(i) + ".in";
ifstream cin(file.c_str());
st = clock();
cin >> n;
for(int j = 1;j <= n;j++)cin >> a[j];
for(int j = 1;j <= n;j++)cout << a[j] << " ";
en = clock();
double tmp = (double)(en - st) / CLOCKS_PER_SEC;
sum += tmp;
cerr << "#" << i << " cin/cout used time: " << fixed << setprecision(5) << tmp << "s.\n";
}
cerr << "\nAll: cin/cout used time: " << fixed << setprecision(5) << sum << "s.\n";
cerr << "Average: cin/cout used time: " << fixed << setprecision(5) << sum / 5 << "s.";
}
测试结果(Windows):
代码
#1 cin/cout used time: 0.00000s.
#2 cin/cout used time: 0.00000s.
#3 cin/cout used time: 0.02400s.
#4 cin/cout used time: 0.17300s.
#5 cin/cout used time: 1.57100s.
All: cin/cout used time: 1.76800s.
Average: cin/cout used time: 0.35360s.
Linux:
总用时:597ms;平均用时:119ms。
scanf/printf 代码:
C++
#include<bits/stdc++.h>#define int long long#define endl '\n'typedef long long ll;
using namespace std;
const int N = 3e7 + 5;
int n,a[N];
clock_t st,en;
double sum;
signed main(){
freopen("output.txt","w",stdout);
for(int i = 1;i <= 5;i++){
string file = "test" + to_string(i) + ".in";
freopen(file.c_str(),"r",stdin);
st = clock();
scanf("%lld",&n);
for(int j = 1;j <= n;j++)scanf("%lld",&a[j]);
for(int j = 1;j <= n;j++)printf("%lld ",a[j]);
en = clock();
double tmp = (double)(en - st) / CLOCKS_PER_SEC;
sum += tmp;
cerr << "#" << i << " scanf/printf used time: " << fixed << setprecision(5) << tmp << "s.\n";
}
cerr << "\nAll: scanf/printf used time: " << fixed << setprecision(5) << sum << "s.\n";
cerr << "Average: scanf/printf used time: " << fixed << setprecision(5) << sum / 5 << "s.";
}
测试结果(Windows):
代码
#1 scanf/printf used time: 0.00000s.
#2 scanf/printf used time: 0.00900s.
#3 scanf/printf used time: 0.08200s.
#4 scanf/printf used time: 0.69100s.
#5 scanf/printf used time: 7.05500s.
All: scanf/printf used time: 7.83700s.
Average: scanf/printf used time: 1.56740s.
Linux:
总用时:790ms;平均用时:158ms。
快读快写代码:
C++
#include<bits/stdc++.h>#define int long long#define endl '\n'typedef long long ll;
using namespace std;
const int N = 3e7 + 5;
int n,a[N];
clock_t st,en;
double sum;
int read(){
int t = 1,x = 0;char ch = getchar();
while(!isdigit(ch)){if(ch == '-')t = -1;ch = getchar();}
while(isdigit(ch))x = (x << 1) + (x << 3) + (ch ^ 48),ch = getchar();
return t * x;
}
void write(int x){
if(x < 0)x = -x,putchar('-');
if(x > 9)write(x / 10);
putchar(x % 10 ^ 48);
}
signed main(){
freopen("output.txt","w",stdout);
for(int i = 1;i <= 5;i++){
string file = "test" + to_string(i) + ".in";
freopen(file.c_str(),"r",stdin);
st = clock();
n = read();
for(int j = 1;j <= n;j++)a[j] = read();
for(int j = 1;j <= n;j++)write(a[j]),putchar(' ');
en = clock();
double tmp = (double)(en - st) / CLOCKS_PER_SEC;
sum += tmp;
cerr << "#" << i << " fast read/write used time: " << fixed << setprecision(5) << tmp << "s.\n";
}
cerr << "\nAll: fast read/write used time: " << fixed << setprecision(5) << sum << "s.\n";
cerr << "Average: fast read/write used time: " << fixed << setprecision(5) << sum / 5 << "s.";
}
Linux:
测试结果(Windows):
代码
#1 fast read/write used time: 0.00000s.
#2 fast read/write used time: 0.00000s.
#3 fast read/write used time: 0.02400s.
#4 fast read/write used time: 0.21000s.
#5 fast read/write used time: 2.18400s.
All: fast read/write used time: 2.41800s.
Average: fast read/write used time: 0.48360s.
总用时:472ms;平均用时:94ms。
fread/fwrite 代码:
C++
#include<bits/stdc++.h>#define int long long#define endl '\n'typedef long long ll;
using namespace std;
const int N = 3e7 + 5;
int n,a[N];
clock_t st,en;
double sum;
namespace fastIO{
#define f_inline inline __attribute__((always_inline))
char buf[1 << 21],*p1 = buf,*p2 = buf;
f_inline char gc(){return (p1 == p2 && (p2 = (p1 = buf) + fread(buf,1,1 << 21,stdin),p1 == p2) ? EOF : *p1++);}
f_inline int read(){//读入一个数字 register int x = 0,f = 1;char ch = gc();
while(!isdigit(ch)){if(ch == '-')f = -1;ch = gc();}
while(isdigit(ch)){x = (x << 1) + (x << 3) + (ch ^ 48),ch = gc();}
return f * x;
}
char buffer[1 << 21];int _p1 = -1;const int _p2 = 1 << 21 - 1;
f_inline void flush(){fwrite(buffer,1,_p1 + 1,stdout),_p1 = -1;}
f_inline void putchar(const register char &x){if(_p1 == _p2)flush();buffer[++_p1] = x;}
inline void write(int x){//输出一个数字 if(x < 0){putchar('-'),x = -x;}
int tmp = x / 10;
if(x > 9)write(tmp);
putchar(x - (tmp << 1) - (tmp << 3) ^ 48);
}
};
using namespace fastIO;
signed main(){
freopen("output.txt","w",stdout);
for(int i = 1;i <= 5;i++){
string file = "test" + to_string(i) + ".in";
freopen(file.c_str(),"r",stdin);
st = clock();
n = read();
for(int j = 1;j <= n;j++)a[j] = read();
for(int j = 1;j <= n;j++)write(a[j]),fastIO::putchar(' ');
flush();
en = clock();
tmp += (double)(en - st) / CLOCKS_PER_SEC;
sum += tmp;
cerr << "#" << i << " fread/fwrite used time: " << fixed << setprecision(5) << tmp << "s.\n";
}
cerr << "\nAll: fread/fwrite used time: " << fixed << setprecision(5) << sum << "s.\n";
cerr << "Average: fread/fwrite used time: " << fixed << setprecision(5) << sum / 5 << "s.";
}
测试结果(Windows):
C++
#1 fread/fwrite used time: 0.00100s.
#2 fread/fwrite used time: 0.00100s.
#3 fread/fwrite used time: 0.00800s.
#4 fread/fwrite used time: 0.04000s.
#5 fread/fwrite used time: 0.36400s.
All: fread/fwrite used time: 0.41400s.
Average: fread/fwrite used time: 0.08280s.
Linux:
总用时 276ms;平均用时 53ms。
3.STL的优化
STL,即为 Standard Template Library,标准模板库。STL 的目的是标准化组件,这样就不用重新开发,可以使用现成的组件。
11011101 00001100 10100001 00011101 这就是 int 在 C++ 的存储方式。
每 8 位是一个字节。第一位是符号位,剩余 31 位表示二进制下的数。
比较效率的程序代码:
C++
#include<bits/stdc++.h>#define endl '\n'typedef long long ll;
using namespace std;
template<typename T>
void testInt(string type){
clock_t st,en;
T x = 2;
st = clock();
for(int i = 1;i <= 200000000;i++)x = x * x - 6 + (x / (x - !x));
en = clock();
cout << type << " used time: " << (double)(en - st) / CLOCKS_PER_SEC << "s." << endl;
}
template<typename T>
void testDouble(string type){
clock_t st,en;
T x = 11.4514;
st = clock();
for(int i = 1;i <= 200000000;i++)x = x * x / 3 + (x / (x - !x));
en = clock();
cout << type << " used time: " << (double)(en - st) / CLOCKS_PER_SEC << "s." << endl;
}
template<typename T>
void testOther(string type){
clock_t st,en;
T x = 1;
st = clock();
for(int i = 1;i <= 200000000;i++)x = ((!x) & x) ^ ((~x) | x) << x;
en = clock();
cout << type << " used time: " << (double)(en - st) / CLOCKS_PER_SEC << "s." << endl;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
testOther<bool>("bool");
testInt<char>("char");
testInt<unsigned char>("unsigned char");
testInt<short>("short");
testInt<unsigned short>("unsigned short");
testInt<int>("int");
testInt<unsigned int>("unsigned int");
testInt<long>("long");
testInt<unsigned long>("unsigned long");
testInt<long long>("long long");
testInt<unsigned long long>("unsigned long long");
testDouble<float>("float");
testDouble<double>("double");
testDouble<long double>("long double");
}
测试结果:
代码
bool used time: 0.349s.
char used time: 1.755s.
unsigned char used time: 1.539s.
short used time: 1.941s.
unsigned short used time: 1.945s.
int used time: 1.454s.
unsigned int used time: 1.61s.
long used time: 1.5s.
unsigned long used time: 1.574s.
long long used time: 1.836s.
unsigned long long used time: 2.325s.
float used time: 2.41s.
double used time: 2.695s.
long double used time: 239.015s.
bool 测试用的是位运算,因此会快很多。至于应用吗,待会再讲(
在整形变量**(包括 char)**中,最快的是 int,这一点是母庸置疑的。
在浮点型变量中,最快的是 float,不过 long double 慢的有点离谱了。
5.二进制压位
如果把上面的代码稍微改一下,变成这样:
C++
#include<bits/stdc++.h>#define endl '\n'typedef long long ll;
using namespace std;
template<typename T>
void test(string type){
clock_t st,en;
T x = 1;
st = clock();
for(int i = 1;i <= 200000000;i++)x = ((!x) & x) ^ ((~x) | x) << x;
en = clock();
cout << type << " used time: " << (double)(en - st) / CLOCKS_PER_SEC << "s." << endl;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
test<bool>("bool");
test<short>("short");
test<unsigned short>("unsigned short");
test<int>("int");
test<unsigned int>("unsigned int");
test<long>("long");
test<unsigned long>("unsigned long");
test<long long>("long long");
test<unsigned long long>("unsigned long long");
}
这样,运行的结果就会变成:
代码
bool used time: 0.354s.
short used time: 0.717s.
unsigned short used time: 0.681s.
int used time: 0.175s.
unsigned int used time: 0.185s.
long used time: 0.184s.
unsigned long used time: 0.191s.
long long used time: 0.329s.
unsigned long long used time: 0.335s.
这时,你可能就会有疑问了:为什么 bool 类型速度这么慢,几乎不敌所有数据类型?
上一章谈到,计算机中使用二进制位来存储数据的。
而 bool 存储在计算机中,浪费了很多字节,因此才会慢。(待考证)
因此,有一种想法就是:利用位运算运行极快的性质,将多个 bit 压成一个 unsigned long long 的数。(只要是 unsigned 就可以)
不过,万能的 STL 还是帮我们实现了二进制压位。就是 bitset。
bitset 会将每八个 bit 压成一个 byte。这样,就实现了时间、空间常数的同时除以 8。
根据这个原理,我们也可以将每八个 bit 压成一个 unsigned long long。这里测试一下效率。
数据生成器:
C++
#include<bits/stdc++.h>#define endl '\n'typedef long long ll;
using namespace std;
uniform_int_distribution<int> dis1(0,1);
uniform_int_distribution<int> dis2(1,3);
uniform_int_distribution<int> dis3(1,10000);
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
freopen("a.txt","w",stdout);
mt19937_64 rand(time(0));
int n = 10000,q = 10000;
cout << n << " " << q << endl;
while(n--)cout << dis1(rand) << " ";
while(q--){
cout << dis2(rand) << " " << dis3(rand) << endl;
}
}
手写:
C++
#include<bits/stdc++.h>#define endl '\n'typedef long long ll;
using namespace std;
const int SIZE = 1e6 + 5;
struct Bitset{
unsigned long long b[SIZE];
void set(){memset(b,0xff,sizeof(b));}
void set(int pos){b[pos >> 6] |= (1ull << pos);}
void reset(){memset(b,0,sizeof(b));}
void reset(int pos){b[pos >> 6] &= ~(1ull << pos);}
bool test(int pos){return (b[pos >> 6] & (1ull << pos));}
}b;
int n,q,opt,x;
double sum;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
freopen("a.txt","w",stdout);
clock_t st,en;
st = clock();
cin >> n >> q;
for(int i = 1;i <= n;i++){
cin >> x;
if(x == 1)b.set(x);
}
while(q--){
cin >> opt >> x;
if(opt == 1)b.set(x);
else if(opt == 2)b.reset(x);
else cout << b.test(x) << endl;
}
en = clock();
sum += (en - st);
cout << fixed << setprecision(5) << (en - st) * 1.00 / CLOCKS_PER_SEC << "s" << endl;
}
输出:0.05200s。
STL bitset:
C++
#include<bits/stdc++.h>#define endl '\n'typedef long long ll;
using namespace std;
const int SIZE = 1e6 + 5;
bitset<SIZE> b;
int n,q,opt,x;
double sum;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
freopen("a.txt","r",stdin);
clock_t st,en;
st = clock();
cin >> n >> q;
for(int i = 1;i <= n;i++){
cin >> x;
if(x == 1)b.set(x);
}
while(q--){
cin >> opt >> x;
if(opt == 1)b.set(x);
else if(opt == 2)b.reset(x);
else cout << b.test(x) << endl;
}
en = clock();
sum += (en - st);
cout << fixed << setprecision(5) << (en - st) * 1.00 / CLOCKS_PER_SEC << "s" << endl;
}