多线程+缓存计算

?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

using
System;

using
System.Collections.Generic;

using
System.Linq;

using
System.Text;

using
System.Management;

using
System.Threading.Tasks;

namespace
TaskTool

{

    using
System.Collections;

    using
System.Collections.Concurrent;

    using
System.Diagnostics;

    using
System.Threading;

    class
Program

    {

        private
int TotalNum;

        private
const int MAXDICNUM = 40000000;

        private
static void Main(string[] args)

        {

            var
stopWath = new
Stopwatch();

            var
allCells=new
List<Point>();

            var
cash = new
ConcurrentDictionary<DisCashPair, short>();

          

            InitialCells(allCells);

            stopWath.Start();

            Cal(allCells, cash, 0, allCells.Count);

            stopWath.Stop();

            Console.WriteLine(string.Format("Single CPU {0},Cash num{1}", stopWath.ElapsedMilliseconds, cash.Count));

            cash.Clear();

            stopWath.Reset();

            stopWath.Start();

            var
cts = new
CancellationTokenSource();

            var
tf = new
TaskFactory(cts.Token, TaskCreationOptions.AttachedToParent,

                                       TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);

            var
blockNum = (int)Math.Ceiling(allCells.Count*1.0/CPUTool.GetCpuNum());

            var
calTasks = new
Task[CPUTool.GetCpuNum()];

            for
(int
i = 0; i < CPUTool.GetCpuNum(); i++)

            {

                var
startNum = i * blockNum;

                var
endNum = (i + 1) * blockNum;

                if
(endNum >= allCells.Count)

                {

                    endNum = allCells.Count - 1;

                }

                calTasks[i] =

                    Task.Factory.StartNew(() => Cal(allCells,cash, startNum, endNum));

            }

            var
endTask = tf.ContinueWhenAll(calTasks, (tasks) =>{});

            endTask.Wait(cts.Token);

            //Task.WaitAll(calTasks.ToArray());

            stopWath.Stop();

            Console.WriteLine(string.Format("Single CPU {0},Cash num{1}", stopWath.ElapsedMilliseconds, cash.Count));

            //Console.WriteLine(string.Format("Single CPU {0},Cash num{1}", stopWath.ElapsedMilliseconds, cash.Count));

        }

        //private static Dictionary<DisCashPair, short> Finish(Task<Dictionary<DisCashPair, short>>[] tasks)

        //{

        //    IEnumerable<KeyValuePair<DisCashPair, short>> result = new Dictionary<DisCashPair, short>();

        //    result = tasks.Select(t => t.Result).Aggregate(result, (current, dic) => current.Union(dic));

        //    return result.ToDictionary(r => r.Key, r => r.Value);

        //}

        private
static ConcurrentDictionary<DisCashPair, short> Cal(List<Point> allCells, int
start, int
end)

        {

            var
cash = new
ConcurrentDictionary<DisCashPair, short>();

            for
(int
i = start; i < end; i++)

            {

                var
cell = allCells[i];

                CalDis(cell, allCells, cash);

            }

            return
cash;

        }

        private
static void Cal(List<Point> allCells, ConcurrentDictionary<DisCashPair, short> cash, int
start, int
end)

        {

            for
(int
i = start; i < end; i++)

            {

                var
cell = allCells[i];

                CalDis(cell, allCells, cash);

            }

        }

        private
static void CalDis(Point cell, List<Point> allCells, ConcurrentDictionary<DisCashPair, short> cash)

        {

            foreach
(var
desCell in
allCells)

            {

                short
dis;

                if
(cash.TryGetValue(new
DisCashPair(cell, desCell), out
dis))

                {

                    continue;

                }

                else

                {

                    cash[new
DisCashPair(cell, desCell)] = GetDis(cell,desCell);

                }

            }

        }

        private
static short GetDis(Point cell, Point desCell)

        {

            var
difX = (cell.X - desCell.X);

            var
difY = (cell.Y - desCell.Y);

            var
dis = Math.Sqrt(difX*difX + difY*difY);

            return
(short) (Math.Round(dis, 2)*100);

        }

        private
static void InitialCells(List<Point> allCells)

        {

            var
rd = new
Random();

            for
(int
i = 0; i < 5000; i++)

            {

                allCells.Add(new
Point()

                {

                    ID=i,

                    X = rd.NextDouble(),

                    Y = rd.NextDouble()

                });

            }

        }

    }

    public
class DisCashPair

    {

        public
Point Src;

        public
Point Des;

        public
DisCashPair(Point src,Point des)

        {

            Src = src;

            Des = des;

        }

        public
override int GetHashCode()

        {

            return
(Src.ID*397) ^ (Des.ID*397);

        }

        public
override bool Equals(object
obj)

        {

            if
(ReferenceEquals(obj, null)) return
false;

            if
(obj.GetType() != typeof
(DisCashPair)) return
false;

            return
Equals((DisCashPair) obj);

        }

        private
bool Equals(DisCashPair pair)

        {

            return
(pair.Src.ID == this.Src.ID && pair.Des.ID == this.Des.ID) ||

                   (pair.Src.ID == this.Des.ID && pair.Des.ID == this.Src.ID);

        }

    }

    public
struct Point

    {

        public
int ID;

        public
double X;

        public
double Y;

    }

    public
static class CPUTool

    {

        private
static int CPUNUM=Int32.MaxValue;

        private
const string SEARCHSR = "Select * from Win32_Processor";

        private
const string CORESNUM = "NumberOfCores";

        public
static int GetCpuNum()

        {

            if
(CPUNUM == Int32.MaxValue)

            {

                CPUNUM =

                    new
System.Management.ManagementObjectSearcher(SEARCHSR).Get()

                        .Cast<ManagementBaseObject>()

                        .Sum(item => int.Parse(item[CORESNUM].ToString()));

            }

            return
CPUNUM;

        }

    }

}

  

using System;using System.Collections.Generic;using System.Linq;using
System.Text;using System.Management;using System.Threading.Tasks;
namespace
TaskTool{    using System.Collections;    using
System.Collections.Concurrent;    using System.Diagnostics; 
  using System.Threading;
    class Program   
{        private int TotalNum;       
private const int MAXDICNUM = 40000000;
        private
static void Main(string[] args)        {     
      var stopWath = new Stopwatch();
     
      var allCells=new List<Point>();     
      var cash = new ConcurrentDictionary<DisCashPair,
short>();                 
    InitialCells(allCells);
         
  stopWath.Start();            Cal(allCells,
cash, 0, allCells.Count);           
stopWath.Stop();           
Console.WriteLine(string.Format("Single CPU {0},Cash num{1}",
stopWath.ElapsedMilliseconds, cash.Count));
       
    cash.Clear();           
stopWath.Reset();           
stopWath.Start();
            var cts = new
CancellationTokenSource();            var tf = new
TaskFactory(cts.Token, TaskCreationOptions.AttachedToParent,     
                     
         
 TaskContinuationOptions.ExecuteSynchronously,
TaskScheduler.Default);
            var
blockNum = (int)Math.Ceiling(allCells.Count*1.0/CPUTool.GetCpuNum());
 
          var calTasks = new
Task[CPUTool.GetCpuNum()];
            for (int
i = 0; i < CPUTool.GetCpuNum(); i++)           
{                var startNum = i *
blockNum;                var endNum = (i
+ 1) * blockNum;                if
(endNum >= allCells.Count)             
  {                   
endNum = allCells.Count - 1;             
  }
                calTasks[i]
=                   
Task.Factory.StartNew(() => Cal(allCells,cash, startNum, endNum)); 
          }
         
  var endTask = tf.ContinueWhenAll(calTasks, (tasks) =>{});   
        endTask.Wait(cts.Token);       
    //Task.WaitAll(calTasks.ToArray());
       
    stopWath.Stop();           
Console.WriteLine(string.Format("Single CPU {0},Cash num{1}",
stopWath.ElapsedMilliseconds, cash.Count));         
  //Console.WriteLine(string.Format("Single CPU {0},Cash num{1}",
stopWath.ElapsedMilliseconds, cash.Count));

     
  }
        //private static
Dictionary<DisCashPair, short> Finish(Task<Dictionary<DisCashPair,
short>>[] tasks)        //{       
//    IEnumerable<KeyValuePair<DisCashPair, short>> result
= new Dictionary<DisCashPair, short>();
        //
   result = tasks.Select(t => t.Result).Aggregate(result, (current,
dic) => current.Union(dic));
        //  
 return result.ToDictionary(r => r.Key, r => r.Value);   
    //}

        private static
ConcurrentDictionary<DisCashPair, short> Cal(List<Point> allCells,
int start, int end)        {       
    var cash = new ConcurrentDictionary<DisCashPair,
short>();            for (int i = start; i <
end; i++)            {       
        var cell = allCells[i];       
        CalDis(cell, allCells, cash);     
      }            return
cash;        }
        private static
void Cal(List<Point> allCells, ConcurrentDictionary<DisCashPair,
short> cash, int start, int end)        {   
        for (int i = start; i < end; i++)   
        {             
  var cell = allCells[i];             
  CalDis(cell, allCells, cash);           
}        }
        private static
void CalDis(Point cell, List<Point> allCells,
ConcurrentDictionary<DisCashPair, short> cash)       
{            foreach (var desCell in
allCells)            {       
        short dis;           
    if (cash.TryGetValue(new DisCashPair(cell, desCell), out
dis))                {   
                continue;   
            }         
      else               
{                    cash[new
DisCashPair(cell, desCell)] = GetDis(cell,desCell);       
        }            } 
      }
        private static short
GetDis(Point cell, Point desCell)        {   
        var difX = (cell.X - desCell.X);     
      var difY = (cell.Y - desCell.Y);       
    var dis = Math.Sqrt(difX*difX + difY*difY);
   
        return (short) (Math.Round(dis, 2)*100); 
      }
        private static void
InitialCells(List<Point> allCells)        { 
          var rd = new Random();
   
        for (int i = 0; i < 5000; i++)   
        {             
  allCells.Add(new Point()             
  {                   
ID=i,                    X =
rd.NextDouble(),                 
  Y = rd.NextDouble()             
  });            }       
}    }
    public class DisCashPair    { 
      public Point Src;        public Point
Des;
        public DisCashPair(Point src,Point
des)        {            Src =
src;            Des = des;     
  }
        public override int GetHashCode() 
      {            return
(Src.ID*397) ^ (Des.ID*397);        }
   
    public override bool Equals(object obj)       
{            if (ReferenceEquals(obj, null))
return false;            if (obj.GetType() !=
typeof (DisCashPair)) return false;           
return Equals((DisCashPair) obj);        }
   
    private bool Equals(DisCashPair pair)       
{            return (pair.Src.ID == this.Src.ID
&& pair.Des.ID == this.Des.ID) ||         
         (pair.Src.ID == this.Des.ID &&
pair.Des.ID == this.Src.ID);        }   
}
    public struct Point    {       
public int ID;        public double X;     
  public double Y;    }
    public static class
CPUTool    {        private static int
CPUNUM=Int32.MaxValue;
        private const string
SEARCHSR = "Select * from Win32_Processor";        private
const string CORESNUM = "NumberOfCores";
        public
static int GetCpuNum()        {       
    if (CPUNUM == Int32.MaxValue)         
  {                CPUNUM = 
                  new
System.Management.ManagementObjectSearcher(SEARCHSR).Get()     
                 
.Cast<ManagementBaseObject>()           
            .Sum(item =>
int.Parse(item[CORESNUM].ToString()));           
}
            return CPUNUM;   
    }
    }}

时间: 2024-08-03 21:00:11

多线程+缓存计算的相关文章

java多线程之计算数量

package Thread.Abort; import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; //计数类 class Count {//DATA priv

每天一个JavaScript实例-使用缓存计算(memoization)来提高应用程序性能

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>每天一个JavaScript实例-使用缓存计算(memoization)来提高应用程序性能</title> <script> window.onload = functio

swift项目开发中缓存计算以及清除

// // KMCacheTool.swift // StopSmokingPrograms // // Created by Fran on 15/10/15. // Copyright © 2015年 kimree. All rights reserved. // import UIKit class KMCacheTool: NSObject { // 计算缓存大小 static var cacheSize: String{ get{ let basePath = NSSearchPath

多线程缓存事例

注:文章示例由上而下,安全级别越高. 示例1. public interface Computable<A,V>{ V compute(A arg) throws InterruptedException; } public class ExpensiveFunction implements Computable<String,BigInteger>{ public BigInteger compute(String arg){ //在经过长时间的计算后 return new B

清除缓存,,计算文件夹大小

#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // //    NSLog(@"缓存文件大小为%@",[NSString stringWithFormat:@"%0.2fM",[self folderSizeAtPath:[N

多线程用于计算1到1000000000之间的数字累加,并比较多个线程耗时多少

package cn.java.core.ch03.job.job03; import java.util.Scanner; public class MultiCalc { private long startTime = 0L; private long endTime =0L; private long totalResult = 0L; private Boolean[] isCompleted = null; public static void main(String[] args)

多线程IO操作(扫描文件夹并计算总大小)

场景为,给到一个硬盘上文件或文件夹,(当然文件夹时,多线程的优势才能越发体现出来),得到该文件或文件夹的大小和计算该结果所需要的时间. 首先是单线程下的例子,这个可难不倒大家,代码如下: 01 public class TotalFileSizeSequential { 02   private long getTotalSizeOfFilesInDir(final File file) { 03     if (file.isFile()) return file.length(); 04  

多线程与网络之SDWebImage和NSCache

*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } a { color: #4183C4; } a.absent { color: #cc0000; } a.anchor { display: block; padding-left: 30px; margin-left: -30px; cursor: pointer; position: absolute

Android 框架修炼-自己封装双缓存管理框架库

一.概述 Android开发中,网络请求是很重要的一部分,而缓存网络请求来的图片或者响应结果字符串或者结果流,既可以省流量,同时也可以帮助我们 解决无网或弱网情况下加载情况,当然也可以提升程序性能效率.纵所周知,缓存管理中肯定需要用到内存缓存,这里我们采用LruCache来管理内存的缓存. LruCahce虽然速度快,但是只是内存级别的缓存,为了实现持久化的缓存,我们还需要文件级别的缓存,也就是说我们要把缓存保存到文件,而文件则是保存 到手机存储或者SD卡存储中,即实现Disk级别的缓存,这里我